mysql日期和字串相互轉化

2021-08-09 23:27:45 字數 1468 閱讀 2781

目的:整理mysql中日期和字串的相互轉化的一些函式

一、日期轉字串

1、函式:date_format(date, format)

2、例:

select date_format(now(),'%y-%m-%d %h:%i:%s');

結果:2017-10-29 14:02:54

select date_format(now(),'%y-%m-%d %h:%i:%s');

結果:2017-10-29 14:02:54

二、日期轉時間戳

1、函式:unix_timestamp(data)

2、例:

select unix_timestamp(now());  

結果:1509257408

select unix_timestamp(now());

結果:1509257408

三、字串轉日期

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

2、例:

select str_to_date('2017-10-29', '%y-%m-%d %h:%i:%s');

結果:2017-10-29 00:00:00

select str_to_date('2017-10-29', '%y-%m-%d %h:%i:%s');

結果:2017-10-29 00:00:00

四、時間戳轉日期

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

2、例:

select from_unixtime(1509257408); 

結果:2017-10-29 14:10:08

select from_unixtime(1509257408);

結果:2017-10-29 14:10:08

五、時間戳轉字串

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

2、例:

select from_unixtime(1509257408,'%y~%m~%d %h:%i:%s'); 

結果:2017~10~29 14:02:08

select from_unixtime(1509257408,'%y~%m~%d %h:%i:%s');

結果:2017~10~29 14:02:08

附錄:

mysql日期格式化(format)取值範圍。

參考:

iOS中日期和字串的相互轉化

例1 從伺服器段獲取到的字串轉化為時間如 轉化1416882712000 網路請求獲取的資料 nsstring time nsstringstringwithformat datelistobjectatindex indexpath.row gradedate nsinteger num time...

日期 字串 相互轉換

獲取系統日期 sysdate 格式化日期 to char sysdate yy mm dd hh24 mi ss 或 to date sysdate yy mm dd hh24 mi ss 格式化數字 to number 注 to char 把日期或數字轉換為字串 to char number,格式...

datetime與字串相互轉化

之前處理資料時,獨取出來的時間資料自動轉化成了str形式,我總是忘記處理的語句,在此做個筆記提醒自己。python標準庫包含用於日期 date 和時間 time 資料的資料型別,而且還有日曆方面的功能。我們主要會用到datetime time以及calendar模組。datetime.datetim...