MySQL:日期函數、時間函數處理

來源:互聯網
上載者:User

標籤:span   cond   轉換函式   style   相減   strong   make   mon   int   

 

date_add() 增加
MYSQL 擷取目前時間加上一個月

update user set leverstart=now(),leverover=date_add(NOW(), interval 1 MONTH) where id=1;

date_sub()減少

date_sub(‘1998-01-01 00:00:00‘, interval ‘1 1:1:1‘ day_second)

month 月份
minute 分鐘
second 秒
hour 小時
week 周
quarter 刻
year 年
獲得目前時間:now();sysdate()
獲得目前時間戳函數:current_timestamp, current_timestamp()
日期/時間 轉換為字串函數:date_format(date,format), time_format(time,format)
字串轉換為日期函數:str_to_date(str, format)
日期、天數 轉換函式:to_days(date), from_days(days)
時間、秒 轉換函式:time_to_sec(time), sec_to_time(seconds)

select time_to_sec(‘01:00:00‘); -- 3600select sec_to_time(3600); -- ‘01:00:00‘

 

 拼湊日期、時間函數:makdedate(year,dayofyear), maketime(hour,minute,second)

select makedate(1901,30); -- ‘1901-01-30‘select makedate(1901,33); -- ‘2001-02-02‘select maketime(11,35,30); -- ‘11:35:30‘

 

日期、時間相減函數:datediff(date1,date2), timediff(time1,time2)

select datediff(‘2008-08-01‘, ‘2008-08-08‘); -- -7select timediff(‘2008-08-08 08:08:08‘, ‘2008-08-08 00:00:00‘); -- 08:08:08select timediff(‘08:08:08‘, ‘00:00:00‘); -- 08:08:08

 

時間戳記(timestamp)轉換、增、減函數:
timestamp(date) -- date to timestamp
timestamp(dt,time) -- dt + time
timestampadd(unit,interval,datetime_expr) --

timestampdiff(unit,datetime_expr1,datetime_expr2) --

select timestamp(‘2008-08-08‘); -- 2008-08-08 00:00:00select timestamp(‘2008-08-08 08:00:00‘, ‘01:01:01‘); -- 2008-08-08 09:01:01select timestamp(‘2008-08-08 08:00:00‘, ‘10 01:01:01‘); -- 2008-08-18 09:01:01select timestampadd(day, 1, ‘2008-08-08 08:00:00‘); -- 2008-08-09 08:00:00select date_add(‘2008-08-08 08:00:00‘, interval 1 day); -- 2008-08-09 08:00:00

 

timestampadd() 函數

select timestampdiff(year,‘2002-05-01‘,‘2001-01-01‘); -- -1select timestampdiff(day ,‘2002-05-01‘,‘2001-01-01‘); -- -485select timestampdiff(hour,‘2008-08-08 12:00:00‘,‘2008-08-08 00:00:00‘); -- -12select datediff(‘2008-08-08 12:00:00‘, ‘2008-08-01 00:00:00‘); -- 7

 

timestampdiff() 函數就比 datediff() 功能強,datediff() 只能計算兩個日期(date)之間相差的天數。

 

 

 

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.