MYSQL日期函數

來源:互聯網
上載者:User

標籤:

#獲得當前日期+時間(date + time)函數
select now()

#獲得目前時間戳函數
select current_timestamp, current_timestamp()


#Date/Time to Str(日期/時間轉換為字串)函數
select date_format(‘2008-08-08 22:23:01‘, ‘%Y%m%d%H%i%s‘)


#字串轉換為日期函數
select str_to_date(‘08/09/2008‘, ‘%m/%d/%Y‘); -- 2008-08-09
select str_to_date(‘08/09/08‘ , ‘%m/%d/%y‘); -- 2008-08-09
select str_to_date(‘08.09.2008‘, ‘%m.%d.%Y‘); -- 2008-08-09
select str_to_date(‘08:09:30‘, ‘%h:%i:%s‘); -- 08:09:30
select str_to_date(‘08.09.2008 08:09:30‘, ‘%m.%d.%Y %h:%i:%s‘); -- 2008-08-09 08:09:30


#(日期、天數)轉換函式
select to_days(‘0000-00-00‘); -- 0
select to_days(‘2008-08-08‘); -- 733627


#(時間、秒)轉換函式
select time_to_sec(‘01:00:05‘); -- 3605
select sec_to_time(3605); -- ‘01:00:05‘

#拼湊日期、時間函數
select makedate(2001,31); -- ‘2001-01-31‘
select makedate(2001,32); -- ‘2001-02-01‘
select maketime(12,15,30); -- ‘12:15:30‘


#(Unix 時間戳記、日期)轉換函式
select unix_timestamp(); -- 1218290027
select unix_timestamp(‘2008-08-08‘); -- 1218124800
select unix_timestamp(‘2008-08-08 12:30:00‘); -- 1218169800

select from_unixtime(1218290027); -- ‘2008-08-09 21:53:47‘
select from_unixtime(1218124800); -- ‘2008-08-08 00:00:00‘
select from_unixtime(1218169800); -- ‘2008-08-08 12:30:00‘

select from_unixtime(1218169800, ‘%Y %D %M %h:%i:%s %x‘); -- ‘2008 8th August 12:30:00 2008‘


#日期、時間相減函數
datediff(date1,date2):兩個日期相減 date1 - date2,返回天數。
select datediff(‘2008-08-08‘, ‘2008-08-01‘); -- 7
select datediff(‘2008-08-01‘, ‘2008-08-08‘); -- -7


#timediff(time1,time2):兩個日期相減 time1 - time2,返回 time 差值。
select timediff(‘2008-08-08 08:08:08‘, ‘2008-08-08 00:00:00‘); -- 08:08:08
select timediff(‘08:08:08‘, ‘00:00:00‘); -- 08:08:08


#指定日期1秒後 interval
SELECT DATE_ADD(‘2010-12-31 23:59:59‘, INTERVAL 1 SECOND)


#指定日期1天后:
SELECT DATE_ADD(‘2010-12-31 23:59:59‘, INTERVAL 1 DAY);



#定日期減去10小時:
SELECT DATE_ADD(‘2011-01-01 00:00:00‘, INTERVAL ‘-1 10‘ DAY_HOUR);


#指定日期的一個月前
SELECT DATE_SUB(‘2011-01-02‘, INTERVAL 31 DAY);


#指定日期的前一天:
SELECT date_add(‘2011-01-01‘, INTERVAL -1 DAY);




MYSQL日期函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.