MySQL time difference function and date conversion calculation function

MySQL's time difference function (TIMESTAMPDIFF, DATEDIFF), date conversion calculation function (date_add, day, date_format, str_to_date)

  1. Time difference function (TIMESTAMPDIFF, DATEDIFF)

You need to use MySQL to calculate the time difference, use TIMESTAMPDIFF, DATEDIFF, and record the experimental results.

--0
select datediff(now(), now());
 
--2
select datediff('2023-09-22 23:59:00', '2023-09-20 00:00:00');
 
--2
select datediff('2023-09-22 00:00:00', '2023-09-20 23:59:00');
 
--1
select TIMESTAMPDIFF(DAY, '2023-09-20 23:59:00', '2023-09-22 00:00:00');
 
--2
select TIMESTAMPDIFF(DAY, '2023-09-20 00:00:00', '2023-09-22 00:00:00');
 
--2
select TIMESTAMPDIFF(DAY, '2023-09-20 00:00:00', '2023-09-22 12:00:00');
 
--2
select TIMESTAMPDIFF(DAY, '2023-09-20 00:00:00', '2023-09-22 23:59:00');
 
--71
select TIMESTAMPDIFF(HOUR, '2023-09-20 00:00:00', '2023-09-22 23:00:00');
 
--4260
select TIMESTAMPDIFF(MINUTE, '2023-09-20 00:00:00', '2023-09-22 23:00:00');
  1. Date conversion calculation function (date_add, day, date_format, str_to_date)

DATE_ADD() function usage

Definition and Usage: The DATE_ADD() function adds a specified time interval to a date.

DATE_ADD(date,INTERVAL expr type)

The date parameter is a legal date expression. The expr parameter is the time interval you wish to add.
The type parameter can be the following values: DAY, WEEK, etc.

DATE_ADD("2023-09-20 ",INTERVAL 2 day)

结果:"2023-09-22 "
DATE_ADD("2023-09-20 ",INTERVAL -2 day)

结果:"2023-09-18 "

date_format(): Returns the formatted time and date

grammar:

DATE_FORMAT(date,format);

parameter:

date: the valid time and date that needs to be formatted

format: A string in a predefined format, with a percent sign (%) in front of each specifier.

SELECT id,
       name,
       DATE_FORMAT(start_time, '%Y-%m-%d %H:%i:%S') start_time,
       DATE_FORMAT(end_time, '%Y-%m-%d %H:%i:%S')   end_time
FROM user

The STR_TO_DATE function is used to convert a string into a date/time value

The functions of the STR_TO_DATE function and the DATE_FORMAT function are opposite.

Syntax format

STR_TO_DATE(str,format)

srt: string to be formatted as date (input string)

format: the format string to use

If str cannot be parsed according to format, the STR_TO_DATE function will return NULL

If any of the parameters is NULL, the STR_TO_DATE function returns NULL

Predefined description character list:
Insert image description here
commonly used format formats
Insert image description here

Practice example
Convert string to DATE value


-- 2023-09-20
SELECT STR_TO_DATE('20,9,2023','%d,%m,%Y');
-- 2023-09-20
SELECT STR_TO_DATE('2023-09-20 11:30:00','%Y-%m-%d');
-- 2023-08-01
SELECT STR_TO_DATE('August,1,2023', '%M,%e,%Y');
-- 2023-08-10
SELECT STR_TO_DATE('August 10 2023', '%M %d %Y');
-- 2023-08-15
SELECT STR_TO_DATE('Monday, August 15, 2023', '%W,%M %e, %Y');

将字符串转换为DATETIME-- 2023-09-20 11:30:00
SELECT STR_TO_DATE('20230920 1130','%Y%m%d %h%i');
-- 2023-09-20 11:30:00
SELECT STR_TO_DATE('2023-09-20 11:30:00','%Y-%m-%d %H:%i:%s');
-- 2023-09-20 10:40:10
SELECT STR_TO_DATE('2023,9,20 10,40,10', '%Y,%m,%d %h,%i,%s');
STR_TO_DATE函数在根据格式字符串format解析输入字符串str时,忽略输入字符串str末尾的额外字符

-- 2023-09-20
SELECT STR_TO_DATE('20,9,2023 extra characters','%d,%m,%Y'); 
-- 2023-09-20 11:30:00
SELECT STR_TO_DATE('20230920 1130 extra characters','%Y%m%d %h%i');
如果输入字符串str是非法的,则STR_TO_DATE函数返回NULL

-- NULL
SELECT STR_TO_DATE('2023','%Y');
-- NULL
SELECT STR_TO_DATE('11','%h');
-- NULL
SELECT STR_TO_DATE('1130','%h%i');
-- NULL
SELECT STR_TO_DATE('113005','%h%i%s');
-- NULL
SELECT STR_TO_DATE('August,20,2023', '%M %e %Y');
-- NULL
SELECT STR_TO_DATE('Monday, August 20, 2023', '%W %M %e %Y');

-- 用日期与字符串转换,计算当月第一天、下月第一天
select curdate() as '当前日期', 
DATE_FORMAT(curdate(), '%Y-%m') as '当前月份', 
str_to_date(concat(DATE_FORMAT(curdate(), '%Y-%m'), '-01'), '%Y-%m-%d') as '当前月的第一天', 
date_add(str_to_date(concat(DATE_FORMAT(curdate(), '%Y-%m'), '-01'), '%Y-%m-%d'), interval 1 month) as '下月的第一天';
 
-- 当前月的最后一天
select last_day(curdate());
 
-- 下月第一天
select date_add(last_day(curdate()), interval 1 day);
 
-- 当天为当月的第几天
select day(curdate());
 
-- 当月第一天
select date_add(curdate(), interval 1-(day(curdate())) day);

Guess you like

Origin blog.csdn.net/weixin_45817985/article/details/133089189