MySQL 时间戳和时间的获取/相互转换/格式化

注:以下sql 测试时间为 2019-06-27 下午

获取当前时间戳(1561614814)
select unix_timestamp();
select unix_timestamp(now());
select unix_timestamp(SYSDATE());


获取当前时间(2019-06-27 13:52:39)
select now();
select SYSDATE();

获取当前日期(2019-06-27)
SELECT CURDATE();

时间转时间戳
select unix_timestamp('2019-06-27 13:52:39');

时间戳转时间(2019-06-27 13:53:34)
select from_unixtime(1561614814);

时间戳格式化(2019-06-27 13:53:34)
SELECT from_unixtime(1561614814, '%Y-%m-%d %H:%i:%S');

时间格式化(2019-06-27)
select date_format(now(), '%Y-%m-%d');
select date_format('2019-06-27 13:53:34', '%Y-%m-%d');


FROM_UNIXTIME 语法:FROM_UNIXTIME(unix_timestamp,format)
返回表示 Unix 时间标记的一个字符串,根据format字符串格式化。format可以包含与DATE_FORMAT()函数列出的条目同样的修饰符。

下列修饰符可以被用在format字符串中:

%M 月名字(January……December)
%b 缩写的月份名字(Jan……Dec)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
select date_format(now(), '%M');//June
select date_format(now(), '%b'); // Jun
select date_format(now(), '%m');//06
select date_format(now(), '%c');//6


%W 星期名字(Sunday……Saturday)
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%a 缩写的星期名字(Sun……Sat)
SELECT from_unixtime(1561614814, '%W');//Thursday
select date_format(now(), '%w');//4
select date_format(now(), '%a');//Thu


%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
SELECT from_unixtime(1561614814, '%D');//27th


%Y 年, 数字, 4 位
%y 年, 数字, 后 2 位
SELECT from_unixtime(1561614814, '%Y'); //2019
SELECT from_unixtime(1561614814, '%y'); //19


%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%j 一年中的天数(001……366)
SELECT from_unixtime(1561614814, '%d'); //27
SELECT from_unixtime(1561614814, '%e'); //27
SELECT from_unixtime(1561614814, '%j'); //178

%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
SELECT from_unixtime(1561614814, '%H'); //13
SELECT from_unixtime(1561614814, '%k'); //13
SELECT from_unixtime(1561614814, '%h'); //01
SELECT from_unixtime(1561614814, '%I'); //01
SELECT from_unixtime(1561614814, '%l'); //1


%i 分钟, 数字(00……59)
SELECT from_unixtime(1561614814, '%i'); //53


%S 秒(00……59)
%s 秒(00……59)
SELECT from_unixtime(1561614814, '%S'); //34
SELECT from_unixtime(1561614814, '%s'); //34

%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
SELECT from_unixtime(1561614814, '%r'); //01:53:34 PM
SELECT from_unixtime(1561614814, '%T'); //13:53:34

%p AM或PM
SELECT from_unixtime(1561614814, '%p'); //PM

%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
SELECT from_unixtime(1561614814, '%U'); //25
SELECT from_unixtime(1561614814, '%u'); //26

%% 一个文字“%”。
SELECT FROM_UNIXTIME(1234567890, '%%');// %

 

参考文章:https://blog.csdn.net/lilongsy/article/details/79061639 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值