zoukankan      html  css  js  c++  java
  • MYSQL日期函数

    #获得当前日期+时间(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);




  • 相关阅读:
    Mac上如何用命令行修改proxy设置
    Mac上解决访问github慢问题
    Bootstrap布局
    ListView详解
    sql server命名规范
    表的管理与操作
    常用编程技巧和方法
    有联系的jQuery选择器
    sql基础查询语句
    数值特征
  • 原文地址:https://www.cnblogs.com/dengyg200891/p/5839196.html
Copyright © 2011-2022 走看看