MySQL get current date, time, timestamp function

Table of contents

1.MySQL get the current date and time function

1.1 Get the current date (date) function: curdate()

1.2 Get the current time (time) function: curtime()

1.3 Get the current date + time (date + time) function: now()

1.4 Get the current date + time (date + time) function: sysdate()

1.5 Get the current timestamp function: current_timestamp, current_timestamp()

2.MySQL date/time conversion function

2.1 Date/time conversion to string functions: date_format(date,format), time_format(time,format)

2.2 Convert string to date function: str_to_date(str, format)

2.3 Date/day conversion function: to_days(date), from_days(days)

2.4 Time/second conversion function: time_to_sec(time), sec_to_time(seconds)

2.5 Make up date/time functions: makdedate(year,dayofyear), maketime(hour,minute,second)

2.6 Unix timestamp/date conversion functions

3. MySQL date and time calculation function

3.1 Add a time interval to the date: date_add()

3.2 Subtract a time interval from a date: date_sub()

3.3 Date and time subtraction function: datediff(date1,date2), timediff(time1,time2)

3.4 Timestamp (timestamp) conversion, increase and decrease functions:

4.MySQL time zone (timezone) conversion function: convert_tz(dt,from_tz,to_tz)


1.MySQL get the current date and time function

1.1 Get the current date (date) function: curdate()

1.2 Get the current time (time) function: curtime()

1.3 Get the current date + time (date + time) function: now()

1.4 Get the current date + time (date + time) function: sysdate()

sysdate() is similar to now(), the difference is: now() gets the value at the beginning of execution, and sysdate() gets the value dynamically when the function is executed.
See the following example to understand:

1.5 Get the current timestamp function: current_timestamp, current_timestamp()


2.MySQL date/time conversion function

2.1 Date/time conversion to string functions: date_format(date,format), time_format(time,format)

MySQL date and time conversion functions: date_format(date,format), time_format(time,format) can convert a date/time into various string formats.
It is an inverse conversion of the str_to_date(str,format) function.

 

2.2 Convert string to date function: str_to_date(str, format)

str_to_date(str,format) conversion function, which can convert some messy strings into date format.
Alternatively, it can be converted to time as well. "format" can refer to the MySQL manual.

select str_to_date('05/08/2023', '%m/%d/%Y'); -- 2023-05-08
select str_to_date('05/08/2023' , '%m/%d/%y'); -- 2023-05-08
select str_to_date('05.08.2023', '%m.%d.%Y'); -- 2023-05-08
select str_to_date('12:32:24', '%h:%i:%s'); -- 00:32:24
select str_to_date('05.08.2023 12:32:24', '%m.%d.%Y %h:%i:%s'); -- 2023-05-08 12:32:24

2.3 Date/day conversion function: to_days(date), from_days(days)

select to_days('0000-01-01'); -- 1(日期转换为天数)
select to_days(curdate()); -- 739013(日期转换为天数)
select from_days(0); -- 0000-00-00(天数转换为日期)
select from_days(739013); -- 2023-05-08(天数转换为日期)

2.4 Time/second conversion function: time_to_sec(time), sec_to_time(seconds)

select time_to_sec('01:00:05'); -- 3605(时间转换为秒数)
select sec_to_time(3605); -- 01:00:05(秒数转换为时间)

2.5 Make up date/time functions: makdedate(year,dayofyear), maketime(hour,minute,second)

select makedate(2020,31); -- 2020-01-3(拼凑日期)
select makedate(2020,32); -- 2020-02-01(拼凑日期)
select maketime(12,15,30); -- 12:15:30(拼凑时间)

2.6 Unix timestamp/date conversion functions

unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)

select unix_timestamp(); -- 1683544672
select from_unixtime(1683544672); -- 2023-05-08 19:17:52

select unix_timestamp(curdate()); -- 1683475200
select from_unixtime(1683475200); -- 2023-05-08 00:00:00

select unix_timestamp(current_timestamp()); -- 1683544792
select from_unixtime(1683544792); -- 2023-05-08 19:19:52

select from_unixtime(1683544672, '%Y %D %M %h:%i:%s %x'); -- 2023 8th May 07:17:52 2023

3. MySQL date and time calculation function

3.1 Add a time interval to the date: date_add()

set @dt = now();

select @dt;
select date_add(@dt, interval 1 day); -- add 1 day
select date_add(@dt, interval 1 hour); -- add 1 hour
select date_add(@dt, interval 1 minute); -- ...
select date_add(@dt, interval 1 second);
select date_add(@dt, interval 1 microsecond);
select date_add(@dt, interval 1 week);
select date_add(@dt, interval 1 month);
select date_add(@dt, interval 1 quarter);
select date_add(@dt, interval 1 year);

select date_add(@dt, interval -1 day); -- sub 1 day

MySQL adddate(), addtime() functions can be replaced by date_add().

The following is an example of date_add() implementing the addtime() function:

mysql> set @dt = now();

mysql> select date_add(@dt, interval '01:15:30' hour_second);
+------------------------------------------------+
| date_add(@dt, interval '01:15:30' hour_second) |
+------------------------------------------------+
| 2023-05-08 20:47:09                            |
+------------------------------------------------+

mysql> select date_add(@dt, interval '1 01:15:30' day_second);
+-------------------------------------------------+
| date_add(@dt, interval '1 01:15:30' day_second) |
+-------------------------------------------------+
| 2023-05-09 20:47:09                             |
+-------------------------------------------------+

3.2 Subtract a time interval from a date: date_sub()

The MySQL date_sub() datetime function is used in the same way as date_add().

mysql> set @dt = now();

mysql> select date_sub(@dt, interval '1 1:1:1' day_second);
+----------------------------------------------------------------+
| date_sub('2020-07-24 12:02:05', interval '1 1:1:1' day_second) |
+----------------------------------------------------------------+
| 2023-05-07 18:32:28                                            |
+----------------------------------------------------------------+

3.3 Date and time subtraction function: datediff(date1,date2), timediff(time1,time2)

MySQL datediff(date1, date2): subtract date1 - date2 from two dates, and return the number of days.

mysql> select datediff(curdate(), '2023-05-01'); -- 7
mysql> select datediff('2023-05-01', '2023-05-08'); -- -7

MySQL timediff(time1,time2): subtract time1 - time2 from two dates, and return the time difference.

mysql> select timediff(current_timestamp(), '2023-05-08 00:00:00'); -- 19:39:20
mysql> select timediff('19:39:20', '00:00:00'); -- 19:39:20

Note: The two parameters of the timediff(time1,time2) function must be of the same type.

3.4 Timestamp (timestamp) conversion, increase and decrease functions:

timestamp(date) – date to timestamp
timestamp(dt,time) – dt + time
timestampadd(unit,interval,datetime_expr)
timestampdiff(unit,datetime_expr1,datetime_expr2)
 

-- 转换
select timestamp('2023-05-08'); -- 2023-05-08 00:00:00
select timestamp('2023-05-08 19:52:51', '01:01:01'); -- 2023-05-08 20:53:52
select timestamp(current_timestamp(), '10 01:01:01'); -- 2023-05-18 20:54:46

-- 增
select timestampadd(day, 1, current_timestamp()); -- 2023-05-09 19:54:26
select date_add(current_timestamp(), interval 1 day); -- 2023-05-09 19:55:06

-- 减
select timestampdiff(year,current_timestamp(),'2018-01-01'); -- -5
select timestampdiff(day ,'2023-05-08','2018-01-01'); -- -1953
select timestampdiff(hour,'2023-05-08 12:00:00','2023-05-08 00:00:00'); -- -12
select datediff('2023-05-08 12:00:00', '2023-05-01 00:00:00'); -- 7

# MySQL timestampdiff() 函数就比 datediff() 功能强多了,datediff() 只能计算两个日期(date)之间相差的天数。

4.MySQL time zone (timezone) conversion function: convert_tz(dt,from_tz,to_tz)

select convert_tz('2023-05-08 12:00:00', '+08:00', '+00:00'); -- 2023-05-08 04:00:00

# 时区转换也可以通过 date_add, date_sub, timestampadd 来实现。
select date_add('2023-05-08 12:00:00', interval -8 hour); -- 2023-05-08 04:00:00
select date_sub('2023-05-08 12:00:00', interval 8 hour); -- 2023-05-08 04:00:00
select timestampadd(hour, -8, '2023-05-08 12:00:00'); -- 2023-05-08 04:00:00

Guess you like

Origin blog.csdn.net/wangshiqi666/article/details/130564629