MySQL 日期与字符串互相转换总结
符号
阅读:759
2021-03-31 12:55:26
评论:0
第一种情况:日期转字符串
1、函数:date_format(date, format)
2、实战:
select date_format(now(),'%Y-%m-%d %H:%i:%S');
第二种情况:日期转时间戳
1、函数:unix_timestamp(data)
2、实战
select unix_timestamp(now());
第三种情况:字符串转日期
1、函数:str_to_date(str,format);注:format格式必须和str的格式相同,否则返回空
2、实战
select str_to_date('2020-12-20', '%Y-%m-%d %H:%i:%S');
第四种情况:时间戳转日期
1、函数:from_unixtime(time-stamp);
2、实战
select from_unixtime(1509257408);
第五种情况:时间戳转日期
1、函数:from_unixtime(time-stamp,format);
2、实战
select from_unixtime(1509257408,'%Y~%m~%d %H:%i:%S');
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。