mysql 日期轉換和數字格式轉換

2021-06-01 10:30:09 字數 1334 閱讀 9279

//時間轉成年月日時分秒

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

//時間轉成年月日

select date_format(now(),'%y%m%d')

//去年此時

select date_add(now(), interval -1 year)

//上月此時

select date_add(now(), interval -1 month)

//昨天此時

select date_add(now(), interval -1 day)

//一小時前

select date_add(now(), interval -1 hour)

//一分鐘前

select date_add(now(), interval -1 minute)

//一秒鐘前

select date_add(now(), interval -1 second)

//昨天(年月日)

select date_format(date_add(now(), interval 1 day),'%y%m%d')

//上個月第一天和最後一天

select date_sub(date_sub(date_format(now(),'%y%m%d'),interval extract( day from now())-1 day),interval 1 month);

select date_sub(date_sub(date_format(now(),'%y%m%d'),interval extract(day from now()) day),interval 0 month);

//某個字串

select date_format(date_add('20090605123020', interval 20 minute),'%y%m%d')

//第幾周

select weekofyear( now() )

select weekofyear('20090606')

在mysql

中,會把'20090707123050'和'20090707'格式的字串作為date型別轉換。

在mysql中,沒有類似oracle

的to_char(num,format)函式,所以涉及到數字前面補0的情況需要特殊處理。

如select left(concat('00'),@num),3)就會顯示三位數字的字串, @num=1時顯示001,為123是顯示123。

concat(year(a.createtime),left(concat('0',weekofyear(a.createtime)),2))

iOS 日期轉換格式(字串轉日期格式)

g 公元時代,例如ad公元 yy 年的後2位 yyyy 完整年 mm 月,顯示為1 12 mmm 月,顯示為英文月份簡寫,如 jan mmmm 月,顯示為英文月份全稱,如 janualy dd 日,2位數表示,如02 d 日,1 2位顯示,如 2 eee 簡寫星期幾,如sun eeee 全寫星期幾,...

python 日期 格式轉換 英文 日期格式轉換

一 date型轉字串 filter date date,yyyymm filter date time,hh mm 在控制器中使用必須注入 filter 模組 scope.dt1 new date 控制器中使用 scope.dt2 filter date scope.dt1,yyyy mm dd h...

關於日期 數字的格式轉換

oracle to char 把日期或數字轉換為字串 to char number,格式 to char salary,99,999.99 to char date,格式 to date 把字串轉換為資料庫中的日期型別 to date char,格式 to number 將字串轉換為數字 to nu...