MySQL 日期与字符串互相转换总结

骑猪看日落 2023-10-10 12:08 105阅读 0赞

第一种情况:日期转字符串

1、函数:date_format(date, format)

2、实战:

  1. select date_format(now(),'%Y-%m-%d %H:%i:%S');

20201220110748941.png

第二种情况:日期转时间戳

1、函数:unix_timestamp(data)

2、实战

  1. select unix_timestamp(now());

20201220110853978.png

第三种情况:字符串转日期

1、函数:str_to_date(str,format);注:format格式必须和str的格式相同,否则返回空

2、实战

  1. select str_to_date('2020-12-20', '%Y-%m-%d %H:%i:%S');

20201220111031226.png

第四种情况:时间戳转日期

1、函数:from_unixtime(time-stamp);

2、实战

  1. select from_unixtime(1509257408);

20201220111155442.png

第五种情况:时间戳转日期

1、函数:from_unixtime(time-stamp,format);

2、实战

  1. select from_unixtime(1509257408,'%Y~%m~%d %H:%i:%S');

20201220111505268.png

发表评论

表情:
评论列表 (有 0 条评论,105人围观)

还没有评论,来说两句吧...

相关阅读