当前位置:网站首页>Built in function time date function
Built in function time date function
2022-07-27 20:12:00 【Hua Weiyun】
11.4 Date and time functions
MySQL A large number of date and time functions are built in , Be flexible 、 Easily process date and time data , This section briefly introduces MySQL Date and time functions built in .
11.4.1 CURDATE() function
CURDATE() The function returns the current date , Only years 、 month 、 Some day , The format is YYYY-MM-DD. An example is as follows :
mysql> SELECT CURDATE();+------------+| CURDATE() |+------------+| 2019-12-11 |+------------+1 row in set (0.00 sec)CURRENT_DATE() Functions and CURDATE() The same function , I won't repeat .
11.4.2 CURTIME() function
CURTIME() The function returns the current time , When only 、 branch 、 Second part , The format is HH:MM:SS. An example is as follows :
mysql> SELECT CURTIME();+-----------+| CURTIME() |+-----------+| 11:27:44 |+-----------+1 row in set (0.00 sec)CURRENT_TIME() Functions and CURTIME The same function , I won't repeat .
11.4.3 NOW() function
NOW() The function returns the current date and time , Include year 、 month 、 Japan 、 when 、 branch 、 second , The format is YYYY-MM-DD HH:MM:SS. An example is as follows :
mysql> SELECT NOW();+---------------------+| NOW() |+---------------------+| 2019-12-15 11:29:22 |+---------------------+1 row in set (0.00 sec)CURRENT_TIMESTAMP() function 、LOCALTIME() function 、LOCALTIMESTAMP() function 、SYSDATE() Functions and NOW() The same function , I won't repeat .
11.4.4 UNIX_TIMESTAMP(date) function
take date Turn into UNIX Time stamp . An example is as follows :
mysql> SELECT UNIX_TIMESTAMP(now());+-----------------------+| UNIX_TIMESTAMP(now()) |+-----------------------+| 1576380910 |+-----------------------+1 row in set (0.01 sec)mysql> SELECT UNIX_TIMESTAMP(CURDATE());+---------------------------+| UNIX_TIMESTAMP(CURDATE()) |+---------------------------+| 1576339200 |+---------------------------+1 row in set (0.00 sec)mysql> SELECT UNIX_TIMESTAMP(CURTIME());+---------------------------+| UNIX_TIMESTAMP(CURTIME()) |+---------------------------+| 1576380969 |+---------------------------+1 row in set (0.00 sec)11.4.5 FROM_UNIXTIME(timestamp) function
FROM_UNIXTIME(timestamp) Function will UNIX Time stamp is converted to date time , The format is YYYY-MM-DD HH:MM:SS, And UNIX_TIMESTAMP(date) Functions are inverse functions to each other . An example is as follows :
mysql> SELECT FROM_UNIXTIME(1576380910);+---------------------------+| FROM_UNIXTIME(1576380910) |+---------------------------+| 2019-12-15 11:35:10 |+---------------------------+1 row in set (0.00 sec)11.4.6 UTC_DATE() function
UTC_DATE() Function to return UTC date . An example is as follows :
mysql> SELECT UTC_DATE();+------------+| UTC_DATE() |+------------+| 2019-12-15 |+------------+1 row in set (0.00 sec)You can also return YYYYMMDD Format date . An example is as follows :
mysql> SELECT UTC_DATE()+0;+--------------+| UTC_DATE()+0 |+--------------+| 20191215 |+--------------+1 row in set (0.00 sec)11.4.7 UTC_TIME() function
UTC_TIME() Function to return UTC Time . An example is as follows :
mysql> SELECT UTC_TIME();+------------+| UTC_TIME() |+------------+| 06:39:00 |+------------+1 row in set (0.00 sec)11.4.8 YEAR(date) function
YEAR(date) The function returns the year of the date , Value returned as 1970~2069. An example is as follows :
mysql> SELECT YEAR(NOW());+-------------+| YEAR(NOW()) |+-------------+| 2019 |+-------------+1 row in set (0.00 sec)Be careful :00~69 Will be converted to 2000~2069,70~99 Will be converted to 1970~1999.
11.4.9 MONTH(date) function
MONTH(date) Function is used to return the month corresponding to the date , Value returned as 1~12. An example is as follows :
mysql> SELECT MONTH(NOW());+--------------+| MONTH(NOW()) |+--------------+| 12 |+--------------+1 row in set (0.00 sec)11.4.10 MONTHNAME(date) function
MONTHNAME(date) Function is used to return the English name of the month where the date is located . An example is as follows :
mysql> SELECT MONTHNAME(NOW());+------------------+| MONTHNAME(NOW()) |+------------------+| December |+------------------+1 row in set (0.00 sec)11.4.11 DAY(date) function
DAY(date) The function returns only the date . An example is as follows :
mysql> SELECT DAY(NOW());+------------+| DAY(NOW()) |+------------+| 15 |+------------+1 row in set (0.00 sec)11.4.12 DAYNAME(date) function
DAYNAME(date) Function is used to return the English name of the week corresponding to the date . An example is as follows :
mysql> SELECT DAYNAME(NOW());+----------------+| DAYNAME(NOW()) |+----------------+| Sunday |+----------------+1 row in set (0.00 sec)mysql> SELECT DAYNAME('2020-01-01');+-----------------------+| DAYNAME('2020-01-01') |+-----------------------+| Wednesday |+-----------------------+1 row in set (0.00 sec)11.4.13 DAYOFWEEK(date) function
DAYOFWEEK(date) Function is used to return the index value of the week corresponding to the date .1 Means Sunday ,2 For Monday , And so on . An example is as follows :
mysql> SELECT DAYOFWEEK(NOW());+------------------+| DAYOFWEEK(NOW()) |+------------------+| 1 |+------------------+1 row in set (0.00 sec)mysql> SELECT DAYOFWEEK('2020-01-01');+-------------------------+| DAYOFWEEK('2020-01-01') |+-------------------------+| 4 |+-------------------------+1 row in set (0.00 sec)11.4.14 WEEKDAY(date) function
WEEKDAY(date) Function returns the index value of the week corresponding to the date .0 For Monday ,1 For Tuesday , And so on . An example is as follows :
mysql> SELECT WEEKDAY(NOW());+----------------+| WEEKDAY(NOW()) |+----------------+| 6 |+----------------+1 row in set (0.00 sec)mysql> SELECT WEEKDAY('2020-01-01');+-----------------------+| WEEKDAY('2020-01-01') |+-----------------------+| 2 |+-----------------------+1 row in set (0.00 sec)11.4.15 WEEK(date) function
WEEK(date) Function returns the week number of a given date in a year . An example is as follows :
mysql> SELECT WEEK(NOW());+-------------+| WEEK(NOW()) |+-------------+| 50 |+-------------+1 row in set (0.00 sec)11.4.16 WEEKOFYEAR(date) function
WEEKOFYEAR(date) Function returns the week ordinal of the year . An example is as follows :
mysql> SELECT WEEKOFYEAR(NOW());+-------------------+| WEEKOFYEAR(NOW()) |+-------------------+| 50 |+-------------------+1 row in set (0.00 sec)11.4.17 DAYOFYEAR(date) function
DAYOFYEAR(date) The function returns the day of the year . An example is as follows :
mysql> SELECT DAYOFYEAR(NOW());+------------------+| DAYOFYEAR(NOW()) |+------------------+| 349 |+------------------+1 row in set (0.00 sec)11.4.18 DAYOFMONTH(date) function
DAYOFMONTH(date) Function returns the day of the month . An example is as follows :
mysql> SELECT DAYOFMONTH(NOW());+-------------------+| DAYOFMONTH(NOW()) |+-------------------+| 15 |+-------------------+1 row in set (0.00 sec)11.4.19 QUARTER(date) function
QUARTER(date) Function returns the quarter corresponding to the date , The scope is 1~4. An example is as follows :
mysql> SELECT QUARTER(NOW());+----------------+| QUARTER(NOW()) |+----------------+| 4 |+----------------+1 row in set (0.00 sec)11.4.20 HOUR(time) function
HOUR(time) Function returns the hour of the specified time . An example is as follows :
mysql> SELECT HOUR(NOW());+-------------+| HOUR(NOW()) |+-------------+| 11 |+-------------+1 row in set (0.00 sec)11.4.21 MINUTE(time) function
MINUTE(time) The function returns the minutes of the specified time , Value range 0~59. An example is as follows :
mysql> SELECT MINUTE(NOW());+---------------+| MINUTE(NOW()) |+---------------+| 45 |+---------------+1 row in set (0.00 sec)11.4.22 SECOND(time) function
SECOND(time) Function returns the number of seconds of the specified time , Value range 0~59. An example is as follows :
mysql> SELECT SECOND(NOW());+---------------+| SECOND(NOW()) |+---------------+| 22 |+---------------+1 row in set (0.00 sec)11.4.23 EXTRACT(type FROM date) function
EXTRACT(type FROM date) Function returns a specific part of a specified date ,type Specifies the value to be returned . among ,type The values of are shown in the table 11-1 Shown .
surface 11-1 EXTRACT(type FROM date) Function type The value and meaning of


Be careful : When EXTRACT(type FROM date) Function type The value is MINUTE_SECOND when , Represents the return of minutes and seconds , When date The minutes in are 12, The second is 12 when , The result returned is 1212. in other words , Splice the second value directly after the minute .type When the value is other underlined values , Follow the same rule .
An example is as follows :
mysql> SELECT EXTRACT(HOUR_MINUTE FROM NOW());+---------------------------------+| EXTRACT(HOUR_MINUTE FROM NOW()) |+---------------------------------+| 2142 |+---------------------------------+1 row in set (0.00 sec)11.4.24 TIME_TO_SEC(time) function
TIME_TO_SEC(time) Function will time Convert to seconds and return the result value . The conversion formula is : Hours *3600+ minute *60+ second . An example is as follows :
mysql> SELECT TIME_TO_SEC(NOW());+--------------------+| TIME_TO_SEC(NOW()) |+--------------------+| 78774 |+--------------------+1 row in set (0.00 sec)11.4.25 SEC_TO_TIME(seconds) function
SEC_TO_TIME(seconds) Function will seconds The description is converted to include hours 、 Minutes and seconds . An example is as follows :
mysql> SELECT SEC_TO_TIME(78774);+--------------------+| SEC_TO_TIME(78774) |+--------------------+| 21:52:54 |+--------------------+1 row in set (0.12 sec)11.4.26 DATE_ADD(date,INTERVAL expr type) function
DATE_ADD(date,INTERVAL expr type) The function returns and date Difference between INTERVAL The date of the interval , In essence, it is the addition of dates . In this function type Is the type of interval , The interval type is shown in the table 11-2 Shown .
surface 11-2 DATE_ADD(date,INTERVAL expr type) Function type The value of

An example is as follows :
mysql> SELECT DATE_ADD(NOW(), INTERVAL 1 DAY);+---------------------------------+| DATE_ADD(NOW(), INTERVAL 1 DAY) |+---------------------------------+| 2019-12-16 22:04:36 |+---------------------------------+1 row in set (0.00 sec)ADDDATE(date,INTERVAL expr type) Function and DATE_ADD(date,INTERVAL expr type) The function does the same thing , I won't repeat .
11.4.27 DATE_SUB(date,INTERVAL expr type) function
DATE_SUB(date,INTERVAL expr type) The function returns and date Difference between INTERVAL The date of the interval , In essence, it is the subtraction of date , among type The values of are shown in table 11-2. An example is as follows :
mysql> SELECT DATE_SUB(NOW(), INTERVAL 1 DAY);+---------------------------------+| DATE_SUB(NOW(), INTERVAL 1 DAY) |+---------------------------------+| 2019-12-14 22:09:10 |+---------------------------------+1 row in set (0.00 sec)SUBDATE(date,INTERVAL expr type) Function and DATE_SUB(date,INTERVAL expr type) Functions work the same , I won't repeat .
Be careful :DATE_ADD、ADDDATE、DATE_SUB and SUBDATE this 4 Both functions can specify negative values .
11.4.28 ADDTIME(time1,time2) function
ADDTIME(time1,time2) The function returns time1 add time2 Time for . among ,time2 It's an expression , It can also be a number , When time2 When it is a number , It stands for seconds . An example is as follows :
mysql> SELECT ADDTIME(NOW(), 50);+---------------------+| ADDTIME(NOW(), 50) |+---------------------+| 2019-12-15 22:17:47 |+---------------------+1 row in set (0.00 sec)mysql> SELECT ADDTIME(NOW(), '1:1:1');+-------------------------+| ADDTIME(NOW(), '1:1:1') |+-------------------------+| 2019-12-15 23:18:46 |+-------------------------+1 row in set (0.00 sec)ADDTIME(NOW(),'1:1:1') It means that the return is the current time plus 1 Hours 1 branch 1 Seconds later .
ADDTIME(time1,time2) Function time2 The value of can also be negative .
mysql> SELECT ADDTIME(NOW(), '-1:-1:-1');+----------------------------+| ADDTIME(NOW(), '-1:-1:-1') |+----------------------------+| 2019-12-15 22:19:29 |+----------------------------+1 row in set, 1 warning (0.01 sec)ADDTIME(NOW(),'-1:-1:-1') It means to return the current time minus 1 Hours 1 branch 1 Seconds later .
11.4.29 SUBTIME(time1,time2) function
SUBTIME(time1,time2) The function returns time1 subtract time2 After time . among ,time2 It's an expression , It can also be a number , When time2 When it is a number , It stands for seconds . An example is as follows :
mysql> SELECT SUBTIME(NOW(), 50); +---------------------+| SUBTIME(NOW(), 50) |+---------------------+| 2019-12-15 22:23:35 |+---------------------+1 row in set (0.00 sec)mysql> SELECT SUBTIME(NOW(), '1:1:1');+-------------------------+| SUBTIME(NOW(), '1:1:1') |+-------------------------+| 2019-12-15 21:23:50 |+-------------------------+1 row in set (0.00 sec)mysql> SELECT SUBTIME(NOW(), '-1:-1:-1'); +----------------------------+| SUBTIME(NOW(), '-1:-1:-1') |+----------------------------+| 2019-12-15 22:25:11 |+----------------------------+1 row in set, 1 warning (0.00 sec)11.4.30 DATEDIFF(date1,date2) function
DATEDIFF(date1,date2) Function calculates the number of days between two dates . An example is as follows :
mysql> SELECT DATEDIFF(NOW(), '1970-01-01');+-------------------------------+| DATEDIFF(NOW(), '1970-01-01') |+-------------------------------+| 18245 |+-------------------------------+1 row in set (0.00 sec)11.4.31 FROM_DAYS(N) function
FROM_DAYS(N) Function returns from 0000 year 1 month 1 The date of ,N Days later . An example is as follows :
mysql> SELECT FROM_DAYS(366);+----------------+| FROM_DAYS(366) |+----------------+| 0001-01-01 |+----------------+1 row in set (0.00 sec)11.4.32 LAST_DAY(date) function
LAST_DAY(date) The function returns date The date of the last day of the month . An example is as follows :
mysql> SELECT LAST_DAY(NOW());+-----------------+| LAST_DAY(NOW()) |+-----------------+| 2019-12-31 |+-----------------+1 row in set (0.00 sec)11.4.33 MAKEDATE(year,n) function
MAKEDATE(year,n) The function returns a date for a given year and the number of days in the year . An example is as follows :
mysql> SELECT MAKEDATE(2020,1);+------------------+| MAKEDATE(2020,1) |+------------------+| 2020-01-01 |+------------------+1 row in set (0.00 sec)mysql> SELECT MAKEDATE(2020,32);+-------------------+| MAKEDATE(2020,32) |+-------------------+| 2020-02-01 |+-------------------+1 row in set (0.00 sec)11.4.34 MAKETIME(hour,minute,second) function
Set the given hour 、 Minutes and seconds are combined into time and return . An example is as follows :
mysql> SELECT MAKETIME(1,1,1);+-----------------+| MAKETIME(1,1,1) |+-----------------+| 01:01:01 |+-----------------+1 row in set (0.00 sec)11.4.35 PERIOD_ADD(time,n) function
PERIOD_ADD(time,n) The function returns time add n After time . An example is as follows :
mysql> SELECT PERIOD_ADD(20200101010101,1);+------------------------------+| PERIOD_ADD(20200101010101,1) |+------------------------------+| 20200101010102 |+------------------------------+1 row in set (0.00 sec)11.4.36 TO_DAYS(date) function
TO_DAYS(date) Function returns the date date distance 0000 year 1 month 1 Days of the day . An example is as follows :
mysql> SELECT TO_DAYS(NOW());+----------------+| TO_DAYS(NOW()) |+----------------+| 737773 |+----------------+1 row in set (0.00 sec)11.4.37 DATE_FORMAT(date,format) function
DATE_FORMAT(date,format) Function in the specified format format To format the date date. among ,format Common formatting symbols are shown in table 11-3 Shown .
surface 11-3 DATE_FORMAT(date,format) Function format Common formatter


An example is as follows :
mysql> SELECT DATE_FORMAT(NOW(), '%H:%i:%s');+--------------------------------+| DATE_FORMAT(NOW(), '%H:%i:%s') |+--------------------------------+| 22:57:34 |+--------------------------------+1 row in set (0.00 sec)11.4.38 TIME_FORMAT(time,format) function
TIME_FORMAT(time,format) Function in the specified format format To format the date date. among ,format The commonly used format characters are shown in table 11-3. An example is as follows :
mysql> SELECT TIME_FORMAT(NOW(), '%H:%i:%s'); +--------------------------------+| TIME_FORMAT(NOW(), '%H:%i:%s') |+--------------------------------+| 22:59:40 |+--------------------------------+1 row in set (0.00 sec)11.4.39 GET_FORMAT(date_type,format_type) function
GET_FORMAT(date_type,format_type) Function returns the display format of the date string , among date_type Represents the date type ,format_type Indicates the format type . The values of date type and format type are shown in table 11-4 Shown .
surface 11-4 GET_FORMAT The format string returned by the function

An example is as follows :
mysql> SELECT GET_FORMAT(DATE, 'USA');+-------------------------+| GET_FORMAT(DATE, 'USA') |+-------------------------+| %m.%d.%Y |+-------------------------+1 row in set (0.00 sec)11.4.40 STR_TO_DATE(str,format) function
STR_TO_DATE(str,format) Function to string str according to format Format into date or time . among ,format The values of are shown in table 11-3. An example is as follows :
mysql> SELECT STR_TO_DATE('2020-01-01 00:00:00','%Y-%m-%d'); +-----------------------------------------------+| STR_TO_DATE('2020-01-01 00:00:00','%Y-%m-%d') |+-----------------------------------------------+| 2020-01-01 |+-----------------------------------------------+1 row in set, 1 warning (0.00 sec)边栏推荐
- [论文阅读] Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation
- How to run kevinchappell / FormBuilder
- 继华为、联发科之后,这家手机芯片厂商宣布向武汉捐款700万
- Chapter 3 basic operation
- Program design Comprehensive Experiment III
- Unified Modeling Language (UML) specification
- In 2019, the global semiconductor revenue fell by 12% year-on-year, and China's market share ranked first
- 2022 love analysis · smart community manufacturer panoramic report manufacturer solicitation
- 想转行软件测试,先过这三关,包含一份3000字超全测试学习指南
- mysql数据库中的数据如何加密呢?mysql8.0自带新特性
猜你喜欢

Chapter 2 Introduction

Software configuration | tigervnc download, installation and configuration

China business CDP white paper | love Analysis Report

一看就懂的ESLint

‘vite‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件

Online judge output overrun

【OpenBMC 系列】4.启动流程 使用qume模拟ast2600-evb

Product Manager: check where there is an error prompt of "system exception" on the offline

技术分享 | 接口自动化测试中,如何做断言验证?

Codeworks 5 questions per day (average 1500) - day 24
随机推荐
C # network application programming, experiment 2: IP address translation and domain name resolution exercises
Overview of deep active learning 2020
10.31 extended configuration of static route
Use cpolar to build a business website (5)
C background GC cause and effect
Detailed introduction to common coordinate system of cesium
C193: scoring system
什么是多层感知机(什么是多层感知机)
antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key
Built in module 10.18
C语言pow函数(c语言中指数函数怎么打)
#yy关于鱼的英文学习
第2章 入门
Understanding of basic concepts of channel capacity and channel bandwidth
Unified Modeling Language (UML) specification
How to quickly improve the three minute response rate of Tiktok store? What will affect the reply rate of Tiktok store?
C # find perfect numbers, output daffodils and use of classes
Assignment 1 - Hello World ! - Simple thread Creation
内置函数锁相关
Source code analysis of Chang'an chain data storage