余子越的博客
Toggle navigation
余子越的博客
主页
计算机网络
大数据分析
系统与工具
编程之路
容器引擎
作者
归档
标签
MySQL时间使用总结
2018-10-27 16:25:53
76
0
0
yuziyue
# 查看MySQL中time_zone值 `show variables like "%time_zone%";` 下面的值表示,MySQL使用的时候系统的时区,而系统使用的是CST时间。 |variable_name | value | |--|--| |system_time_zone | CST | |time_zone | SYSTEM | # 获取当前日期+时间 下面的几种方法种,如果能用`()`的,都可以精确到毫秒级别,只有在括号内精确到几位即可,比如`select now(6)` - `now();` 获取当前的日期+时间(2018-10-26 16:56:23) <br> - `sysdate()` 获取当前的日期+时间(2018-10-26 16:56:23),为实际的执行时的时间 <br> - `current_timestamp` , `current_timestamp()` 获取当前的日期+时间(2018-10-26 16:56:23) <br> - `localtime, localtime()` 获取当前的日期+时间(2018-10-26 16:56:23) <br> - `localtimestamp, localtimestamp()` 获取当前的日期+时间(2018-10-26 16:56:23) <br> # 获取当前日期 - `curdate()` 获取当前日期 - `current_date, current_date()` 获取当前日期 <br> # 获取当前时间 - `curtime()` - `current_time, current_time()` <br> # 时间格式化 ``` # 方法一 set @dt = '2018-10-22 10:10:23' select date_format(@dt, '%Y-%m-%d %H:%m:%S') # 方法二 set @dt = '2018-10-22 10:10:23' select time_format(@dt, '%Y-%m-%d %H:%m:%S') ``` <br> # 获取日期的各个部分 ``` set @dt = now(6); select @dt; -- 2018-10-27 15:28:28.555145 select date(@dt); -- 2018-10-27 select time(@dt); -- 15:28:28 select year(@dt); -- 2018 select quarter(@dt); -- 4 select month(@dt); -- 10 select week(@dt); -- 42 select day(@dt); -- 27 select hour(@dt); -- 15 select minute(@dt); -- 28 select second(@dt); -- 28 select microsecond(@dt); -- 555145 ``` # 一周的第几天、一个月的第几天、一年的第几天 ``` set @dt = now(6); select dayofweek(@dt) select dayofyear(@dt) select dayofmonth(@dt) ``` <br> # 周几名称,月份名称 ``` set @dt = now(6); select dayname(@dt) select monthname(@dt) ``` <br> # 日期的最后一天 ``` set @dt = now(6); select last_day(@dt) # 根据last_day函数的特性,可以获取每个月总共有多少天 select day(last_day(@dt)) ``` <br> # 日期增加、减去一个时间间隔 ``` set @dt = '2018-10-26 15:47:21'; select date_add(@dt, interval 1 day); select date_add(@dt, interval 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); # 当增加的时间既包含小时,又包含分钟时用下面的方式,一看便知。 set @dt = '2018-10-26 15:47:21'; select date_add(@dt, interval '01 01' day_hour); select date_add(@dt, interval '01 00:01' day_minute); select date_add(@dt, interval '01 00:00:01' day_second); select date_add(@dt, interval '01:01' hour_minute); select date_add(@dt, interval '01:00:01' hour_second); select date_add(@dt, interval '01:01' minute_second); # date_sub 函数表示减去一个时间段,和 date_add 的用法完全相同。 set @dt = '2018-10-26 15:47:21'; select date_sub(@dt, interval 1 day); ``` <br> # 日期、时间相减 ``` # 日期相减返回相差的天数,datediff的值可能为负数 select datediff('2018-08-08', '2018-08-01'); select datediff('2018-08-01', '2018-08-08'); # 时间相减返回相差的时间,timediff函数前面时间必须大于后面时间 select timediff('2018-08-08 08:08:08', '2018-08-08 00:00:00'); select timediff('08:08:08', '00:00:00'); ``` <br> # 日期、时间之间的相互转换 ``` # 时间、秒数之间的转换 select time_to_sec('01:00:01'); select sec_to_time(3601); ``` <br> # 时间戳、字符串之间转换 ``` # 字符串到时间戳。 select unix_timestamp(); select unix_timestamp('2018-08-08'); select unix_timestamp('2018-08-08 12:30:00'); # 时间戳到字符串。同时还可以指定字符串的格式 select from_unixtime(1540628606); -- '2018-10-27 16:23:26' select from_unixtime(1540628606, '%Y-%m-%d %H:%m:%S'); -- '2018-10-27 16:23:26' ``` # 获取UTC时间 - `utc_date, utc_date()` 日期 - `utc_time, utc_time()` 时间 - `utc_timestamp, utc_timestamp()` 日期+时间 <br>
上一篇:
MySQL流程函数
下一篇:
掌握 VMware Workstation 三种网络的原理
0
赞
76 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
文档导航