加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

MySQL的日期和时间处理函数

发布时间:2023-01-29 14:01:16 所属栏目:MsSql教程 来源:转载
导读: 常用的日期和时间处理函数
函数 说明 adddate() 增加一个日期 addtime() 增加一个时间 curdate() 返回当前日期 curtime() 返回当前时间 date() 返回日期时间的日期部分 datediff() 计算两个

常用的日期和时间处理函数

函数 说明 adddate() 增加一个日期 addtime() 增加一个时间 curdate() 返回当前日期 curtime() 返回当前时间 date() 返回日期时间的日期部分 datediff() 计算两个日期之差 date_add() 高度灵活的日期计算函数 date_format() 返回一个格式化的日期和时间串 day() 返回一个日期的天数部分 dayofweek() 对于一个日期返回对应的星期几 hour() 返回一个时间的小时部分 minute() 返回一个时间的分钟部分 month() 返回一个日期的月份部分 second() 返回一个日期的秒部分 time() 返回一个日期时间的时间部分 year() 返回一个日期的年份部分 now() 返回当前的日期和时间

adddate()

它大致可以分为两种用法,第一种是当第一个参数是日期时,第二个参数是整数,这意味着添加到该日期的天数。

[En]

It can be roughly divided into two types of usages, the first, when the first parameter is a date, and the second parameter is an integer, which means the number of days added to that date.

例如:

select ADDDATE('2021-11-07',5);

第二种:是第一参数为日期,第二个参数为任意时间表达式,该表达式可为多少分钟、或者多少毫秒、亦或者多少周等等。第二个参数的写法为 interval 空格 一个整型的时间量 空格 时间单位

例如:

select ADDDATE('2021-11-07',interval 6243150562 second );

select ADDDATE('2021-11-07',interval 5 week );

select ADDDATE('2021-11-07',interval 2 quarter);

addtime()

用法:有两个参数,第一个参数是要添加的日期或时间,第二个参数是增加的时间

[En]

Usage: there are two parameters, the first parameter is the date or time to be added, and the second parameter is the increased time

SELECT ADDTIME('1999-12-31 23:59:59.999999','1 1:1:1.000002');

curdate()

返回的是当前服务器日期

select curdate();

curtime()

返回当前服务器的时间信息

select curtime();

date()

将含有日期和时间的参数返回日期部分

select date('2021-07-07 11:11:20');

datediff()

为了计算两个时间之间的日期差异Mssq日期函数,有两个日期-时间参数,这两个参数将第一个参数的日期部分减去第二个参数的日期部分。如果第一个参数的时间晚于第二个参数的时间(通俗地说,值较大),则差为正,否则为负。

[En]

To calculate the date difference between the two times, there are two date-time parameters, which will take the date part of the first parameter minus the date part of the second parameter. If the time of the first parameter is later than that of the second parameter (popularly speaking, the value is larger), then the difference is positive, otherwise it is negative.

select datediff('2021-11-10 11:11:20','2021-11-07 19:13:21');

select datediff('2021-11-10 11:11:20','2021-12-07 19:13:21');

date_add()

这是一个灵活的日期计算函数。该函数有两个参数,第一个参数是要添加的日期或时间,第二个参数要自由得多。它可以是毫秒、秒、分钟、小时、周、年、季度等,基本上可以代表时间单位。您还可以为负数减去时间。

[En]

This is a flexible date calculation function. This function has two parameters, the first parameter is the date or time to be added, and the second parameter is much freer. It can be millisecond, second, minute, hour, week, year, quarter, and so on, which can basically represent the unit of time. You can also subtract time for a negative number.

例如

SELECT DATE_ADD('1998-01-01 00:00:00',INTERVAL '-3 20' DAY_HOUR);

date_format()

日期格式化函数是根据格式字符串格式化日期值。以下说明符可以在格式字符串中使用。格式说明符前必须有%字符。date_format(日期,格式化符号);

%a 缩写星期名(Sun..Sat) %b 缩写月名 (Jan..Dec) %c 月,数值(0..12) %D 带有英文前缀的月中的天(0th, 1st, 2nd, 3rd, …) %d 月的天,数值(00-31) %e 月的天,数值(0-31) %f 微秒(000000..999999) %H 小时 (00-23) %h 小时 (01-12) %I 小时 (01-12) %i 分钟,数值(00-59) %j 年的天 (001-366) %k 小时 (0-23) %l 小时 (1-12) %M 月名 (January..December) %m 月,数值(00-12) %p AM 或 PM %r 时间,12-小时(hh:mm:ss AM 或 PM) %S 秒(00-59) %s 秒(00-59) %T 时间, 24-小时 (hh:mm:ss) %U 周 (00-53) 星期日是一周的第一天 %u 周 (00-53) 星期一是一周的第一天 %V 周 (01-53) 星期日是一周的第一天,与 %X 使用 %v 周 (01-53) 星期一是一周的第一天,与 %x 使用 %W 星期名(Sunday..Saturday) %w 周的天 (0=星期日, 6=星期六) %X 年,其中的星期日是周的第一天,4 位,与 %V 使用 %x 年,其中的星期一是周的第一天,4 位,与 %v 使用 %Y 年,4 位 %y 年,2 位

例如:

SELECT DATE_FORMAT('2021-11-10 22:23:00', '%W %M %Y');

day()

该函数返回一个时间的天数部分,用法 day(时间);与dayofmonth()函数等价

SELECT day('2021-11-10 23:59:59.999999');

dayofweek()

该函数返回一个时间对应的星期几,用法 dayofweek(时间);

SELECT dayofweek('2021-11-10 23:59:59.999999');

dayofyear()

该函数是返回一年该日期在此年中第多少天

SELECT dayofyear('2021-02-10 23:59:59.999999');

hour()

该函数返回此时间的小时部分

SELECT hour('2021-02-10 23:59:59.999999');

minute()

该函数返回此时间的分钟部分

SELECT minute('2021-02-10 23:59:59.999999');

MySQL的日期和时间处理函数

month()

该函数返回此时间的月份部分

SELECT month('2021-02-10 23:59:59.999999');

second()

该函数返回时间的秒数部分

SELECT second('2021-02-10 23:59:59.999999');

MySQL的日期和时间处理函数

time()

该函数返回日期时间的时间部分

SELECT time ('2021-02-10 23:59:59.999999');

MySQL的日期和时间处理函数

year()

该函数返回日期时间的年份部分

SELECT year ('2021-02-10 23:59:59.999999');

MySQL的日期和时间处理函数

now()

该函数返回当前的日期时间

select now();

Original:

Author: 月染霜华

Title: MySQL的日期和时间处理函数

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!