当前位置:网站首页>Date and time function of MySQL function summary
Date and time function of MySQL function summary
2022-07-26 08:13:00 【Operation and maintenance home】
Date and time functions are mainly used to process date and time values , The general date function uses
dateType , You can also usedatetimeperhapstimestampParameters of type , But the time part of these values is ignored .

Function to get the current date
curdate() and current_date() The function does the same thing , Set the current date according to yyyy-mm-dd or yyyymmdd The value of the format returns .
mysql> select curdate(), current_date(), curdate() + 0;
+------------+----------------+---------------+
| curdate() | current_date() | curdate() + 0 |
+------------+----------------+---------------+
| 2022-07-12 | 2022-07-12 | 20220712 |
+------------+----------------+---------------+
1 row in set (0.00 sec)
mysql>
See from above , The functions of the two functions are the same , Both return the same current system date .
curdate()+0 Converts the current date to numeric .
Function to get the current time
curtime() and current_time() The function does the same thing , Change the current time to hh:mm:ss perhaps hhmmss The format returns .
mysql> select curtime(), current_time(), curtime()+0;
+-----------+----------------+-------------+
| curtime() | current_time() | curtime()+0 |
+-----------+----------------+-------------+
| 21:32:25 | 21:32:25 | 213225 |
+-----------+----------------+-------------+
1 row in set (0.00 sec)
mysql>
See from above , The functions of the two functions are the same , They all go back on the current time of the same system .
Function to get the current date and time
current_timestamp()、localtime()、now() and sysdate() this 4 The functions have the same function , Are values that return the current date and time .
mysql> select current_timestamp(), localtime(), now(), sysdate();
+---------------------+---------------------+---------------------+---------------------+
| current_timestamp() | localtime() | now() | sysdate() |
+---------------------+---------------------+---------------------+---------------------+
| 2022-07-12 21:34:52 | 2022-07-12 21:34:52 | 2022-07-12 21:34:52 | 2022-07-12 21:34:52 |
+---------------------+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)
mysql>
See from above ,4 The results returned by the two functions are the same .
UNIX Time stamp function
UNIX_TIMESTAMP(date) If there is no parameter call , Returns a UNIX Time stamp (‘1970-01-01 00:00:00’GMT Seconds after that ) As an unsigned integer .
among ,GMT(Green wich mean time) Greenwich mean time . If use date To call UNIX_TIMESTAMP(), It will change the parameter value to 1970-01-0100:00:00GMT The number of seconds after the return .
date It could be a DATE character string 、DATETIME character string 、TIMESTAMP Or a local time YYMMDD or YYYYMMDD Number in format .
mysql> select unix_timestamp(), unix_timestamp(now()), now();
+------------------+-----------------------+---------------------+
| unix_timestamp() | unix_timestamp(now()) | now() |
+------------------+-----------------------+---------------------+
| 1657633074 | 1657633074 | 2022-07-12 21:37:54 |
+------------------+-----------------------+---------------------+
1 row in set (0.00 sec)
mysql>
from_unixtime(date) Function unix Time stamp is converted to normal format , And unix_timestamp(date) Functions are inverse functions to each other . Example :
mysql> select from_unixtime('1657633074');
+-----------------------------+
| from_unixtime('1657633074') |
+-----------------------------+
| 2022-07-12 21:37:54.000000 |
+-----------------------------+
1 row in set (0.00 sec)
mysql>
return UTC Function of date
utc_date() Function returns the current utc( World standard time ) Date value , The format for yyyy-mm-dd perhaps yyyymmdd;
mysql> select utc_date(), utc_date()+0;
+------------+--------------+
| utc_date() | utc_date()+0 |
+------------+--------------+
| 2022-07-12 | 20220712 |
+------------+--------------+
1 row in set (0.00 sec)
mysql>
return UTC A function of time
utc_time() Returns the current utc Time value , The format is hh:mm:ss perhaps hhmmss;
mysql> select utc_time(), utc_time()+0;
+------------+--------------+
| utc_time() | utc_time()+0 |
+------------+--------------+
| 13:43:24 | 134324 |
+------------+--------------+
1 row in set (0.00 sec)
mysql>
Function to get month MONTH(date) and MONTHNAME(date)
month(date) The function returns date The corresponding month .
mysql> select month('2022-12-12') as coll, month('20221212') as coll_1, month('221212') as coll_2;
+------+--------+--------+
| coll | coll_1 | coll_2 |
+------+--------+--------+
| 12 | 12 | 12 |
+------+--------+--------+
1 row in set (0.00 sec)
mysql>
monthname(date) Function returns the date date The full English name of the corresponding month ;
mysql> select monthname('2022-12-12'), monthname('20221212'), monthname('221212');
+-------------------------+-----------------------+---------------------+
| monthname('2022-12-12') | monthname('20221212') | monthname('221212') |
+-------------------------+-----------------------+---------------------+
| December | December | December |
+-------------------------+-----------------------+---------------------+
1 row in set (0.00 sec)
mysql>
Get the function of week DAYNAME(d)、DAYOFWEEK(d) and WEEKDAY(d)
dayname(d) The function returns d English name of the corresponding working day ;
mysql> select dayname('2022-07-12');
+-----------------------+
| dayname('2022-07-12') |
+-----------------------+
| Tuesday |
+-----------------------+
1 row in set (0.00 sec)
mysql>
dayofweek(d) The function returns d Index of the corresponding week ( Location ,1 Means Sunday ,2 It means Monday , And so on ,7 It means Saturday );
mysql> select dayofweek('2022-07-12') as coll, dayofweek('2022-07-13') as coll_1;
+------+--------+
| coll | coll_1 |
+------+--------+
| 3 | 4 |
+------+--------+
1 row in set (0.00 sec)
mysql>
weekday(d) return d Corresponding weekday index :0 It means Monday ,1 Tuesday , And so on ,6 Means Sunday .
mysql> select weekday('2022-07-12') as coll, weekday('2022-07-13') as coll_1;
+------+--------+
| coll | coll_1 |
+------+--------+
| 1 | 2 |
+------+--------+
1 row in set (0.00 sec)
mysql>
Get the function of week WEEK(d) and WEEKOFYEAR(d)
week(d) Calculate the date d It's the week of the year .
week() The two parameter form of allows you to specify whether the week begins on Sunday or Monday , And whether the return value range is 053 perhaps 153. if mode Parameters are omitted , Then use default_week_format The value of the system argument , Here's the picture :

for example : Use week() The function queries which week of the year the specified date is ;
mysql> select week('2022-07-13'), week('2022-01-01'), week('2022-09-18');
+--------------------+--------------------+--------------------+
| week('2022-07-13') | week('2022-01-01') | week('2022-09-18') |
+--------------------+--------------------+--------------------+
| 28 | 0 | 38 |
+--------------------+--------------------+--------------------+
1 row in set (0.00 sec)
mysql>
weekofyear(d) Calculate the week of the year in which a day is located , The scope is 1~53, amount to week(d,3);
for example : Use weekofyear(d) The query specifies the week of the year ;
mysql> select weekofyear('2022-07-13'), week('2022-07-13', 3);
+--------------------------+-----------------------+
| weekofyear('2022-07-13') | week('2022-07-13', 3) |
+--------------------------+-----------------------+
| 28 | 28 |
+--------------------------+-----------------------+
1 row in set (0.00 sec)
mysql>
As can be seen from the above , The two functions return the same result .
Function to get the number of days DAYOFYEAR(d) and DAYOFMONTH(d)
dayofyear(d) The function returns d It's the day of the year , The scope is 1~366;
for example : Use dayofyear() Function returns the position of the specified date in a year ;
mysql> select dayofyear('2022-07-13'), dayofyear('2022-01-01');
+-------------------------+-------------------------+
| dayofyear('2022-07-13') | dayofyear('2022-01-01') |
+-------------------------+-------------------------+
| 194 | 1 |
+-------------------------+-------------------------+
1 row in set (0.00 sec)
mysql>
dayofmonth(d) The function returns d It's the day of the month , The scope is 1~31;
for example : Use dayofmonth() Function returns the position of the specified date in a month , There must be a specific year .
mysql> select dayofmonth('2022-07-13'), dayofmonth('220713'), dayofmonth('0713');
+--------------------------+----------------------+--------------------+
| dayofmonth('2022-07-13') | dayofmonth('220713') | dayofmonth('0713') |
+--------------------------+----------------------+--------------------+
| 13 | 13 | NULL |
+--------------------------+----------------------+--------------------+
1 row in set, 1 warning (0.00 sec)
mysql>
Get year 、 quarter 、 Hours 、 As a function of minutes and seconds
year(date) return date Corresponding year , The scope is 1970~2069;
mysql> select year('2022-07-13'), year('20330909');
+--------------------+------------------+
| year('2022-07-13') | year('20330909') |
+--------------------+------------------+
| 2022 | 2033 |
+--------------------+------------------+
1 row in set (0.00 sec)
mysql>
tip :
00~69Convert to2000~2069,70~99Convert to1970~1999.
quarter(date) return date The corresponding quarterly value of the year , The scope is 1~4;
mysql> select quarter('2022-07-13'), quarter('20330101');
+-----------------------+---------------------+
| quarter('2022-07-13') | quarter('20330101') |
+-----------------------+---------------------+
| 3 | 1 |
+-----------------------+---------------------+
1 row in set (0.00 sec)
mysql>
minute(time) return time The corresponding number of minutes , The scope is 0~59.
mysql> select minute('2022-07-13 09:09:09'), minute('06:06:06');
+-------------------------------+--------------------+
| minute('2022-07-13 09:09:09') | minute('06:06:06') |
+-------------------------------+--------------------+
| 9 | 6 |
+-------------------------------+--------------------+
1 row in set (0.00 sec)
mysql>
second(time) return time The corresponding number of seconds , The scope is 0~59.
mysql> select second('2022-07-13 09:09:09'), second('06:06:06');
+-------------------------------+--------------------+
| second('2022-07-13 09:09:09') | second('06:06:06') |
+-------------------------------+--------------------+
| 9 | 6 |
+-------------------------------+--------------------+
1 row in set (0.00 sec)
mysql>
Function to get the specified value of the date EXTRACT(type from date)
extract(type from date) The time interval type specifier used by the function is the same as date_add() or date_sub() identical , But it takes a part from the date , Instead of performing date operations .
mysql> select extract(year from '2022-07-13') as coll, extract(year_month from '2022-07-13') as coll_1, extract(day_minute from '2022-07-13 09:08:07') as coll_2;
+------+--------+--------+
| coll | coll_1 | coll_2 |
+------+--------+--------+
| 2022 | 202207 | 130908 |
+------+--------+--------+
1 row in set (0.00 sec)
mysql>
type For different values , The values returned are different :
year: Only return year valueyear_month: Return year and monthday_minute: Return day 、 Hours and minutes
Time and second conversion function
time_to_sec(time) Returns the number of seconds converted time Parameters .
Change the formula to : Hours *3600+ minute *60+ second
mysql> select time_to_sec('09:09:09');
+-------------------------+
| time_to_sec('09:09:09') |
+-------------------------+
| 32949 |
+-------------------------+
1 row in set (0.00 sec)
mysql>
sec_to_time(seconds) Return is converted into hours 、 Minutes and seconds seconds Parameter values , The format for hh:mm:ss perhaps hhmmss.
mysql> select time_to_sec('09:09:09'), sec_to_time(32949);
+-------------------------+--------------------+
| time_to_sec('09:09:09') | sec_to_time(32949) |
+-------------------------+--------------------+
| 32949 | 09:09:09 |
+-------------------------+--------------------+
1 row in set (0.00 sec)
mysql>
You can see from above ,time_to_sec and sec_to_time They're inverse functions to each other .
A function that calculates the date and time
The functions for calculating date and time are :
date_add()
adddate()
date_sub()
subdate()
addtime()
subtime()
date_diff()
stay date_add(date, interval expr type) and date_sub(date, interval expr type) in :
dateIt's adatetimeperhapsdatevalue , Used to specify the starting time .exprIt's an expression , Used to specify the interval value added or subtracted from the start date . For time intervals with negative values ,exprYou can use a symbol-start .typeFor the keyword , Indicates how the expression is interpreted .
if date The parameter is one date value , The calculation will only include year、month and day part ( There is no time part ), The result is a date value , otherwise , The result will be a datetime value .
date_add(date, interval expr type) and adddate(date, intervar expr type) Two functions have the same function , Perform the addition of dates .
mysql> select date_add('2022-07-13 09:09:09', interval 1 second) as coll, adddate('2022-07-13 09:09:09', interval 1 second) as coll_1, date_add('2022-07-13 09:09:09', interval '1:1' minute_second) as coll_2;
+---------------------+---------------------+---------------------+
| coll | coll_1 | coll_2 |
+---------------------+---------------------+---------------------+
| 2022-07-13 09:09:10 | 2022-07-13 09:09:10 | 2022-07-13 09:10:10 |
+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)
mysql>
date_sub(date, interval expr type) perhaps subdate(date, interval expr type) Two functions have the same function , Perform the subtraction of the date .
mysql> select date_sub('2022-07-13 09:09:09', interval 31 day) as coll, subdate('2022-07-13 09:09:09', interval 31 day) as coll_1, date_sub('2022-07-13 09:09:09', interval '0 0:1:1' day_second) as coll_2;
+---------------------+---------------------+---------------------+
| coll | coll_1 | coll_2 |
+---------------------+---------------------+---------------------+
| 2022-06-12 09:09:09 | 2022-06-12 09:09:09 | 2022-07-13 09:08:08 |
+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)
mysql>
addtime(date, expr) Function will expr Value added to date, And return the modified value ,date Value a date or datetime expression , and expr It's a time expression .
mysql> select adddate('2022-07-13 09:09:09', '1:1:1'), addtime('09:09:09', '1:1:20');
+-----------------------------------------+-------------------------------+
| adddate('2022-07-13 09:09:09', '1:1:1') | addtime('09:09:09', '1:1:20') |
+-----------------------------------------+-------------------------------+
| 2022-07-14 09:09:09 | 10:10:29 |
+-----------------------------------------+-------------------------------+
1 row in set, 1 warning (0.00 sec)
mysql>
subtime(date,expr) Function will date subtract expr value , And return the modified value .
among ,date Is a date or date time expression , and expr It's a time expression .
mysql> select subtime('2020-07-13 09:09:09', '1:1:1 1:1:1'), subtime('2022-07-13 09:09:09', '1:1:1');
+-----------------------------------------------+-----------------------------------------+
| subtime('2020-07-13 09:09:09', '1:1:1 1:1:1') | subtime('2022-07-13 09:09:09', '1:1:1') |
+-----------------------------------------------+-----------------------------------------+
| 2020-07-13 08:08:08 | 2022-07-13 08:08:08 |
+-----------------------------------------------+-----------------------------------------+
1 row in set, 2 warnings (0.00 sec)
mysql>
You can see from above , Only the time is calculated , The date is not counted .
Use datediff() Function to calculate the number of days between two dates ;
mysql> select datediff('2022-07-13 09:09:09', '2022-01-01') as coll, datediff('2022-07-13 09:09:09', '2022-07-10 08:08:08');
+------+--------------------------------------------------------+
| coll | datediff('2022-07-13 09:09:09', '2022-07-10 08:08:08') |
+------+--------------------------------------------------------+
| 193 | 3 |
+------+--------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
Functions that format dates and times
date_format(date,format) according to format Display... In the specified format date value .
The main format The format is as follows :

Example : Use date_format() Function to format the output date and time values ;
mysql> select date_format('2022-07-13 09:08:07', '%W %M %Y') as coll, date_format('2022-07-13 09:08:07', '%D %y %a %d %m %b %j') as coll_1;
+---------------------+---------------------------+
| coll | coll_1 |
+---------------------+---------------------------+
| Wednesday July 2022 | 13th 22 Wed 13 07 Jul 194 |
+---------------------+---------------------------+
1 row in set (0.00 sec)
mysql>
time_format(time, format) According to the expression format The request shows the time time .
expression format Specifies the format of the display . because time_format(time, format) Only processing time , therefore format Only use time format .
Example : Use time_format() Function format input time value .
mysql> select time_format('13:14:15', '%H %k %h %I %l');
+-------------------------------------------+
| time_format('13:14:15', '%H %k %h %I %l') |
+-------------------------------------------+
| 13 13 01 01 1 |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql>
get_format(val_typr, format_type) Return the display format of date time string :
val_typeRepresents the date data type , Includedate、datetimeandtime;format_typeIndicates the format display type , Includeeur、interval、iso、jis、usa.
get_format The display format of the returned string based on the combination of two value types is as follows :
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-rZxWt3uu-1658713395049)(https://mmbiz.qpic.cn/mmbiz_png/Nwec3GEnHPENBJMLpicSyFGj3dA5AE3sZ6vlg9d7fj7oEKibCNazOjE76ibBrbaWBrsBKkOHibCcEia454v0P3ppTag/640?wx_fmt=png&random=0.638224736308207)]
Example : Use get_format() Function to display format strings under different format types .
mysql> select get_format(date, 'eur'), get_format(datetime, 'jis');
+-------------------------+-----------------------------+
| get_format(date, 'eur') | get_format(datetime, 'jis') |
+-------------------------+-----------------------------+
| %d.%m.%Y | %Y-%m-%d %H:%i:%s |
+-------------------------+-----------------------------+
1 row in set (0.00 sec)
mysql>
mysql> select date_format('2022-07-13 09:08:07', get_format(datetime, 'jis')) as coll, date_format('2022-07-13 09:08:07', get_format(date, 'usa')) as coll_1;
+---------------------+------------+
| coll | coll_1 |
+---------------------+------------+
| 2022-07-13 09:08:07 | 07.13.2022 |
+---------------------+------------+
1 row in set (0.00 sec)
mysql>
thus , In this paper, the end .
For more information, go to VX official account “ Operation and maintenance home ” , Get the latest article .
------ “ Operation and maintenance home ” ------
------ “ Operation and maintenance home ” ------
------ “ Operation and maintenance home ” ------
linux Interrupt exception ,linuxuml modeling ,linux Compile multiple source files ,linux How to update the time ,linux Paging video tutorial in ,
linux How to decompress gzip,linux Unzip a directory , How to close linux gateway ,linux Document structure diagram ,linux Improve the permission to execute ,
linux What mark is used in the document , Network security linux strengthening ,linux Check email in ,linux Change the background color ,linux Build a website under the environment .
边栏推荐
猜你喜欢

Let's talk about the three core issues of concurrent programming.

Burp Suite - Chapter 1 burp suite installation and environment configuration

这是一张图片

Burp suite Chapter 9 how to use burp repeater

Burp Suite-第六章 如何使用Burp Spider

SPSS用KMeans、两阶段聚类、RFM模型在P2P网络金融研究借款人、出款人行为规律数据

Burp Suite-第九章 如何使用Burp Repeater

【 fastjson1.2.24反序列化漏洞原理代码分析】

The first ide overlord in the universe, replaced...
分享高压超低噪声LDO测试结果(High Voltage Ultra-low Noise LDO)
随机推荐
Master slave database deployment
Traversal mode of list, set, map, queue, deque, stack
The bigger the project is, the bigger it is. This is how I split it
2022/7/9 exam summary
2w字详解数据湖:概念、特征、架构与案例
小蜜蜂吉他谱 高八度和低八度
Why is Google's internal tools not suitable for you?
SPSS用KMeans、两阶段聚类、RFM模型在P2P网络金融研究借款人、出款人行为规律数据
Introduction to arrays -- array
BGP选路原则
Rewriting and overloading
分享高压超低噪声LDO测试结果(High Voltage Ultra-low Noise LDO)
2022/7/6 exam summary
一键部署LAMP和LNMP架构
2022/7/17 exam summary
Idea settings set shortcut keys to convert English letters to case in strings
mysql函数汇总之日期和时间函数
数组的介绍--Array
[June 29, 2022] examination summary
利用js实现统计字符串数组中各字符串出现的次数,并将其格式化为对象数组。