MySQL時間轉換方法

2021-10-09 04:12:22 字數 1472 閱讀 1013

mysql獲取當前時間的方法為:now()

date_format

(date,

'%y-%m-%d')--

----

----

----

>oracle中的to_char()

;str_to_date

(date,

'%y-%m-%d')--

----

----

----

>oracle中的to_date()

;

date_format() 函式用於以不同的格式顯示日期/時間資料。

語法

date_format

(date,format)

date 引數是合法的日期。format 規定日期/時間的輸出格式。

例:

select  id,

date_format

(create_time,

'%y-%c-%d %h:%i:%s'

)from epc_mes_item

where create_time =

'2019-11-19 23:00:20'

;

str_to_date(str,format)函式是將時間格式的字串(str),按照所提供的顯示格式(format)轉換為datetime型別的值。

例:

select str_to_date

('2020-08-06 10:20:30'

,'%y-%m-%d %h:%i:%s'

) as result;

%y:代表4位的年份

%y:代表2為的年份

%m:代表月, 格式為(

01……12

)%c:代表月, 格式為(

1……12

)%d:代表月份中的天數,格式為(

00……31

)%e:代表月份中的天數, 格式為(

0……31

)%h:代表小時,格式為(

00……23

)%k:代表 小時,格式為(

0……23

)%h: 代表小時,格式為(

01……12

)%i: 代表小時,格式為(

01……12

)%l :代表小時,格式為(

1……12

)%i: 代表分鐘, 格式為(

00……59

)%s:代表秒,格式為(00-

59)%s:代表秒,格式為(00-

59)%f:代表微秒

%r:代表 時間,格式為12 小時(hh:mm:ss [ap]m)

%t:代表 時間,格式為24 小時(hh:mm:ss)

%w 星期名

%w 周的天 (1

=星期日,

7=星期六)

mysql時間轉換

通過當前時間進行運算 獲取當前時間 select time to sec now now current timestamp replace unix timestamp current timestamp 3 當前時間加1毫秒 select date add now interval 1 micr...

mysql 時區 時間轉換 MySQL時間時區轉換

將timestamp轉換為指定時區的時間,如 2018 09 21 11 48 42 select convert tz create time,session.time zone,8 00 from auth user 將timestamp轉換為指定時區的時間,並精確到天數,如 2018 09 2...

mysql 查詢時間轉換 Mysql 查詢時間轉換

利用data format函式 select date format now y m d 根據format字串格式化date值 s,s 兩位數字形式的秒 00,01,59 i,i 兩位數字形式的分 00,01,59 h 兩位數字形式的小時,24 小時 00,01,23 h 兩位數字形式的小時,12 ...