MySQL 5.7关于日期和时间的函数整理

本文详细介绍MySQL中用于操作日期和时间的各种内置函数,包括获取当前日期时间、日期加减运算、格式化日期等实用功能。
 Name                                                               Description
ADDDATE()                                                     Add time values (intervals) to a date value
ADDTIME()                                                     Add time
CONVERT_TZ()                                               Convert from one time zone to another
CURDATE()                                                    Return the current date
CURRENT_DATE(), CURRENT_DATE                   Synonyms for CURDATE()
CURRENT_TIME(), CURRENT_TIME                    Synonyms for CURTIME()
CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP Synonyms for NOW()
CURTIME()                                                    Return the current time
DATE()                                                         Extract the date part of a date or datetime expression
DATE_ADD()                                                 Add time values (intervals) to a date value
DATE_FORMAT()                                            Format date as specified
DATE_SUB()                                                  Subtract a time value (interval) from a date
DATEDIFF()                                                   Subtract two dates
DAY()                                                           Synonym for DAYOFMONTH()
DAYNAME()                                                   Return the name of the weekday
DAYOFMONTH()                                             Return the day of the month (0-31)
DAYOFWEEK()                                               Return the weekday index of the argument
DAYOFYEAR()                                                Return the day of the year (1-366)
EXTRACT()                                                    Extract part of a date
FROM_DAYS()                                                Convert a day number to a date
FROM_UNIXTIME()                                         Format Unix timestamp as a date
GET_FORMAT()                                              Return a date format string
HOUR()                                                        Extract the hour
LAST_DAY                                                    Return the last day of the month for the argument
LOCALTIME(), LOCALTIME                               Synonym for NOW()
LOCALTIMESTAMP, LOCALTIMESTAMP()             Synonym for NOW()
MAKEDATE()                                                 Create a date from the year and day of year
MAKETIME()                                                 Create time from hour, minute, second
MICROSECOND()                                           Return the microseconds from argument
MINUTE()                                                    Return the minute from the argument
MONTH()                                                     Return the month from the date passed
MONTHNAME()                                             Return the name of the month
NOW()                                                        Return the current date and time
PERIOD_ADD()                                             Add a period to a year-month
PERIOD_DIFF()                                             Return the number of months between periods
QUARTER()                                                  Return the quarter from a date argument
SEC_TO_TIME()                                            Converts seconds to 'HH:MM:SS' format
SECOND()                                                    Return the second (0-59)
STR_TO_DATE()                                           Convert a string to a date
SUBDATE()                                                  Synonym for DATE_SUB()  when invoked with three arguments
SUBTIME()                                                   Subtract times
SYSDATE()                                                  Return the time at which the function executes
TIME()                                                        Extract the time portion of the expression passed
TIME_FORMAT()                                           Format as time
TIME_TO_SEC()                                           Return the argument converted to seconds
TIMEDIFF()                                                  Subtract time
TIMESTAMP()                                              With a single argument, this function returns the date or datetime expression; with two arguments, the sum of the arguments
TIMESTAMPADD()                                        Add an interval to a datetime expression
TIMESTAMPDIFF()                                        Subtract an interval from a datetime expression
TO_DAYS()                                                 Return the date argument converted to days
TO_SECONDS()                                           Return the date or datetime argument converted to seconds since Year 0
UNIX_TIMESTAMP()                                     Return a Unix timestamp
UTC_DATE()                                               Return the current UTC date
UTC_TIME()                                                Return the current UTC time
UTC_TIMESTAMP()                                      Return the current UTC date and time
WEEK()                                                     Return the week number
WEEKDAY()                                               Return the weekday index
WEEKOFYEAR()                                          Return the calendar week of the date (1-53)
YEAR()                                                      Return the year
YEARWEEK()                                              Return the year and week

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15498/viewspace-2136023/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15498/viewspace-2136023/

MySQL 5.7提供了丰富的时间函数来处理日期时间。根据官方文档,一些常用的时间函数包括: 1. 获取当前日期时间:使用NOW()、CURRENT_TIMESTAMP()、SYSDATE()或CURRENT_TIMESTAMP函数来获取当前的日期时间值。 2. 获取当前时间:可以使用CURTIME()、CURRENT_TIME()或TIME()函数来获取当前的时间。 3. 时间加减间隔:使用DATE_ADD()函数可以在日期时间上添加一个间隔。可以使用DATE_SUB()函数日期时间上减去一个间隔。 4. 日期时间的格式化:使用DATE_FORMAT()函数可以将日期时间值格式化为特定的格式。 5. 提取时间部分:使用TIME()函数可以提取传递表达式的时间部分。 这些只是MySQL 5.7提供的一部分时间函数,更多的时间函数用法可以参考MySQL官方文档中的详细说明。 引用: MySQL 关于时间函数的官方文档:https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html MySQL 官方文档中的时间函数示例:1.1. 获取 YYYY-MM-DD HH:mm:ss SELECT NOW(),CURRENT_TIMESTAMP(),SYSDATE(),CURRENT_TIMESTAMP<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [MySQL中的时间函数](https://blog.csdn.net/weixin_38192427/article/details/123365010)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值