赞
踩
CURDATE() 、CURRENT_DATE()
例1:获取系统当前日期
- mysql> select curdate();
- +------------+
- | curdate() |
- +------------+
- | 2023-02-14 |
- +------------+
- 1 row in set (0.00 sec)
例2:获取系统当前日期
- mysql> select current_date();
- +----------------+
- | current_date() |
- +----------------+
- | 2023-02-14 |
- +----------------+
- 1 row in set (0.00 sec)
CURTIME() 、CURRENT_TIME()
例:获取系统当前时间,两个函数作用相同
- mysql> select curtime();
- +-----------+
- | curtime() |
- +-----------+
- | 16:15:16 |
- +-----------+
- 1 row in set (0.00 sec)
-
- mysql> select current_time();
- +----------------+
- | current_time() |
- +----------------+
- | 16:15:23 |
- +----------------+
- 1 row in set (0.00 sec)
CURRENT_TIMESTAMP() 、LOCALTIME() 、NOW() 、SYSDATE()
例:获取系统当前日期和时间,四个函数作用相同
- mysql> select current_timestamp();
- +---------------------+
- | current_timestamp() |
- +---------------------+
- | 2023-02-14 16:16:23 |
- +---------------------+
- 1 row in set (0.00 sec)
-
- mysql> select localtime();
- +---------------------+
- | localtime() |
- +---------------------+
- | 2023-02-14 16:17:49 |
- +---------------------+
- 1 row in set (0.00 sec)
-
- mysql> select now();
- +---------------------+
- | now() |
- +---------------------+
- | 2023-02-14 16:17:53 |
- +---------------------+
- 1 row in set (0.00 sec)
-
- mysql> select sysdate();
- +---------------------+
- | sysdate() |
- +---------------------+
- | 2023-02-14 16:18:01 |
- +---------------------+
- 1 row in set (0.00 sec)
UNIX_TIMESTAMP()
例:获取系统当前时间戳
- mysql> select unix_timestamp();
- +------------------+
- | unix_timestamp() |
- +------------------+
- | 1676362724 |
- +------------------+
- 1 row in set (0.00 sec)
FROM_UNIXTIME()
例:将时间戳转换为日期时间
- mysql> select from_unixtime(1676362724);
- +---------------------------+
- | from_unixtime(1676362724) |
- +---------------------------+
- | 2023-02-14 16:18:44 |
- +---------------------------+
- 1 row in set (0.00 sec)
UTC_DATE()
例:获取当前 UTC (世界标准时间) 日期
- mysql> select utc_date();
- +------------+
- | utc_date() |
- +------------+
- | 2023-02-14 |
- +------------+
- 1 row in set (0.00 sec)
UTC_TIME()
例:获取当前UTC(世界标准时间)时间
- mysql> select utc_time();
- +------------+
- | utc_time() |
- +------------+
- | 08:22:05 |
- +------------+
- 1 row in set (0.00 sec)
MONTH(date) 、MONTHNAME(date)
例:返回date对应的月份,返回结果为2,表示为2月
- mysql> select month('2023-02-14');
- +---------------------+
- | month('2023-02-14') |
- +---------------------+
- | 2 |
- +---------------------+
- 1 row in set (0.00 sec)
例2:返回date对应月份,返回结果为february,英文表示2月
- mysql> select monthname('2023-02-14');
- +-------------------------+
- | monthname('2023-02-14') |
- +-------------------------+
- | February |
- +-------------------------+
- 1 row in set (0.00 sec)
DAYNAME(date) 、DAYOFWEEK(date) 、WEEKDAY(date) 、WEEK(date) 、WEEKOFYEAR(date)
例1:返回date对应的工作日英文名称
- mysql> select dayname('2023-02-14');
- +-----------------------+
- | dayname('2023-02-14') |
- +-----------------------+
- | Tuesday |
- +-----------------------+
- 1 row in set (0.00 sec)
例2:返回date对应一周的索引,1表示周日,2表示周一,...,7表示周六
- mysql> select dayofweek('2023-02-14');
- +-------------------------+
- | dayofweek('2023-02-14') |
- +-------------------------+
- | 3 |
- +-------------------------+
- 1 row in set (0.00 sec)
例3:返回date对应一周的索引,0表示周一,1表示周二,...,6表示周日
- mysql> select weekday('2023-02-14');
- +-----------------------+
- | weekday('2023-02-14') |
- +-----------------------+
- | 1 |
- +-----------------------+
- 1 row in set (0.00 sec)
例4:返回date是一年中的第几周,一年有52.1428571429周
- mysql> select week('2023-02-14');
- +--------------------+
- | week('2023-02-14') |
- +--------------------+
- | 7 |
- +--------------------+
- 1 row in set (0.02 sec)
例5:返回date是一个月中的第几天
- mysql> select dayofmonth('2023-02-14');
- +--------------------------+
- | dayofmonth('2023-02-14') |
- +--------------------------+
- | 14 |
- +--------------------------+
- 1 row in set (0.03 sec)
YEAR(date)
例:返回date对应的年份,如没有则返回null
- mysql> select year('11-02-03'),year('90-09-39');
- +------------------+------------------+
- | year('11-02-03') | year('90-09-39') |
- +------------------+------------------+
- | 2011 | NULL |
- +------------------+------------------+
- 1 row in set, 1 warning (0.00 sec)
QUARTER(date)
例:返回date对应的季度,一年四个季度
- mysql> select quarter('11-02-03'),quarter('11-04-03');
- +---------------------+---------------------+
- | quarter('11-02-03') | quarter('11-04-03') |
- +---------------------+---------------------+
- | 1 | 2 |
- +---------------------+---------------------+
- 1 row in set (0.00 sec)
MINUTE(time)
例:返回当前时间的分钟,”now()”为当前时间
- mysql> select minute(now());
- +---------------+
- | minute(now()) |
- +---------------+
- | 48 |
- +---------------+
- 1 row in set (0.00 sec)
SECOND(time)
例:返回当前时间的秒数
- mysql> select second(now());
- +---------------+
- | second(now()) |
- +---------------+
- | 24 |
- +---------------+
- 1 row in set (0.00 sec)
EXTRACT(type FROM date)
例:分别返回年、月、日,type为日期类型,date为时间
- mysql> select extract(year from now());
- +--------------------------+
- | extract(year from now()) |
- +--------------------------+
- | 2023 |
- +--------------------------+
- 1 row in set (0.00 sec)
-
- mysql> select extract(month from now());
- +---------------------------+
- | extract(month from now()) |
- +---------------------------+
- | 2 |
- +---------------------------+
- 1 row in set (0.00 sec)
-
- mysql> select extract(day from now());
- +-------------------------+
- | extract(day from now()) |
- +-------------------------+
- | 15 |
- +-------------------------+
- 1 row in set (0.00 sec)
TIME_TO_SEC(time) 、SEC_TO_TIME(time)
例1:返回从零点到当前时间的秒数
- mysql> select time_to_sec(now());
- +--------------------+
- | time_to_sec(now()) |
- +--------------------+
- | 42959 |
- +--------------------+
- 1 row in set (0.00 sec)
例2:把秒转换为时分秒
- mysql> select sec_to_time(42959);
- +--------------------+
- | sec_to_time(42959) |
- +--------------------+
- | 11:55:59 |
- +--------------------+
- 1 row in set (0.00 sec)
DATE_ADD() 、ADDDATE() 、DATE_SUB() 、SUBDATE() 、ADDTIME() 、SUBTIME() 、DATEDIFF()
例1:DATE_ADD() 用于对日期进行加法运算,对指定的时间增加1分1秒
- mysql> select date_add('2022-12-31 23:59:59',interval '1:1' minute_second);
- +--------------------------------------------------------------+
- | date_add('2022-12-31 23:59:59',interval '1:1' minute_second) |
- +--------------------------------------------------------------+
- | 2023-01-01 00:01:00 |
- +--------------------------------------------------------------+
- 1 row in set (0.00 sec)
例2:ADDDATE()同上,用于对日期的加法运算
- mysql> select adddate('2022-12-31 23:59:59',interval '1:1' minute_second);
- +-------------------------------------------------------------+
- | adddate('2022-12-31 23:59:59',interval '1:1' minute_second) |
- +-------------------------------------------------------------+
- | 2023-01-01 00:01:00 |
- +-------------------------------------------------------------+
- 1 row in set (0.01 sec)
ADDDATE()也可以通过下面语法添加天数,格式如下
- mysql> select adddate('2023-01-01',10);
- +--------------------------+
- | adddate('2023-01-01',10) |
- +--------------------------+
- | 2023-01-11 |
- +--------------------------+
- 1 row in set (0.00 sec)
例3:DATE_SUB()用于对日期的减法运算,如下:2月1日减去31天
- mysql> select date_sub('2023-02-01',interval 31 day);
- +----------------------------------------+
- | date_sub('2023-02-01',interval 31 day) |
- +----------------------------------------+
- | 2023-01-01 |
- +----------------------------------------+
- 1 row in set (0.00 sec)
例4:SUBDATE()同上,对日期进行减法运算
- mysql> select subdate('2023-02-01',interval 31 day);
- +---------------------------------------+
- | subdate('2023-02-01',interval 31 day) |
- +---------------------------------------+
- | 2023-01-01 |
- +---------------------------------------+
- 1 row in set (0.00 sec)
SUBDATE()也可以通过下面语法减少天数,格式如下
- mysql> select subdate('2023-02-01',31);
- +--------------------------+
- | subdate('2023-02-01',31) |
- +--------------------------+
- | 2023-01-01 |
- +--------------------------+
例5:ADDTIME()对日期进行加法运算,给指定的日期增加1时1分1秒
- mysql> select addtime('2023-12-30 23:59:59','1:1:1');
- +----------------------------------------+
- | addtime('2023-12-30 23:59:59','1:1:1') |
- +----------------------------------------+
- | 2023-12-31 01:01:00 |
- +----------------------------------------+
- 1 row in set (0.00 sec)
例6:SUBTIME() 对日期进行减法运算,给指定的日期减去1时1分1秒
- mysql> select subtime('2023-12-30 23:59:59','1:1:1');
- +----------------------------------------+
- | subtime('2023-12-30 23:59:59','1:1:1') |
- +----------------------------------------+
- | 2023-12-30 22:58:58 |
- +----------------------------------------+
- 1 row in set (0.00 sec)
例7:DATEDIFF() 用于计算两个日期之间的间隔天数
- mysql> select datediff('2023-01-31','2023-01-1');
- +------------------------------------+
- | datediff('2023-01-31','2023-01-1') |
- +------------------------------------+
- | 30 |
- +------------------------------------+
- 1 row in set (0.00 sec)
DATE_FORMAT(date, format) 、TIME_FORMAT(time, format) 、GET_FORMAT(val_type, format_type)
格式化类型在下面,根据需求修改
例1:DATE_FORMAT(date, format)用于格式化日期,根据format指定的格式显示date值
- mysql> select date_format('2023-01-31 22:30:00','%W %M %Y');
- +-----------------------------------------------+
- | date_format('2023-01-31 22:30:00','%W %M %Y') |
- +-----------------------------------------------+
- | Tuesday January 2023 |
- +-----------------------------------------------+
- 1 row in set (0.01 sec)
例2:TIME_FORMAT(time, format) 格式化时间,根据format指定的格式显示time值
- mysql> select time_format('16:00:00','%H %k %I');
- +------------------------------------+
- | time_format('16:00:00','%H %k %I') |
- +------------------------------------+
- | 16 16 04 |
- +------------------------------------+
- 1 row in set (0.00 sec)
例3:GET_FORMAT(val_type, format_type)指定值的类型和格式化类型,然后将其转换为字符串
- mysql> select date_format('2023-01-20 22:30:00',get_format(date,'usa'));
- +-----------------------------------------------------------+
- | date_format('2023-01-20 22:30:00',get_format(date,'usa')) |
- +-----------------------------------------------------------+
- | 01.20.2023 |
- +-----------------------------------------------------------+
- 1 row in set (0.00 sec)
类型 | 说明 |
%d | 该月日期,数字形式(00..31) |
%e | 该月日期,数字形式(0..31) |
%f | 微秒(000000...999999) |
%H | 以2位数表示24小时(00..23) |
%h,%I | 以2位数表示12小时(01..12) |
%i | 分钟,数字形式(00-59) |
%j | 一年中的天数(001-366) |
%k | 以24小时(0-23) |
%l | 以12小时(0..12) |
%M | 月份名称(january..December) |
%m | 月份数字形式(00..12) |
%p | 上午(AM)或下午(PM) |
%r | 时间,12小时制(小时hh:分钟mm:秒钟ss后面加AM或PM) |
%S,%s | 以2位数形式表示秒(00..59) |
%T | 时间,24小时制(小时hh:分钟mm:秒数ss) |
%U | 周(00..53),其中周日为每周的第一天 %u周(00..53),其中周一为每周的第一天 |
%V | 周(01..53),其中周日为每周的第一天,和%X一起使用 |
%v | 周(01..53),其中周一为每周的第一天,和%x一起使用 |
%W | 工作日名称(周日..周六) |
%w | 一周中的每日(0=周日..6=周六) |
%X | 该周的年份,其中周日为每周的第一天;数字形式4位数,和%V同时使用 |
%x | 该周的年份,其中周一为每周的第一天;数字形式4位数,和%v同时使用 |
%Y | 4位数形式表示年份 |
%y | 2位数形式表示年份 |
%% | “%”文字字符 |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。