mysql日期和时间格式的相互转换

一、时间戳和字符串的转换

1、日期转字符串

函数:date_format(date, format)

举例:

    select date_format(now(),'%Y-%m-%d %H:%i:%S');
    结果:2017-10-29 14:02:54
    

2、日期转时间戳

函数:unix_timestamp(data)

举例:

    select unix_timestamp(now());  
    结果:1509257408
    

 

 

 

3、字符串转日期

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

举例:

select str_to_date('2017-10-29 10:01:01', '%Y-%m-%d %H:%i:%S')

 结果:2017-10-29 10:01:01
select str_to_date('2017-10-29 ', '%Y-%m-%d %H:%i:%S')

结果:2017-10-29 00:00:00
 

4、时间戳转日期

函数:from_unixtime(time-stamp);

举例:

   select from_unixtime(1509257408); 
   结果:2017-10-29 14:10:08

5、时间戳转字符串

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

举例:

    select from_unixtime(1509257408,'%Y~%m~%d %H:%i:%S'); 
    结果:2017~10~29 14:02:08

 
posted @   一朵盛开的腊梅花  阅读(7732)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示