• MySQL中的时间问题


    MySQL 获得当前日期时间 函数

    获得当前日期+时间(date + time)函数:now()

    mysql> select now();
    
    +---------------------+
    | now() |
    +---------------------+
    | 2008-08-08 22:20:46 |
    +---------------------+

    获得当前日期+时间(date + time)函数:sysdate()
    sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。看下面的例子就明白了:

    mysql> select now(), sleep(3), now();
    
    +---------------------+----------+---------------------+
    | now() | sleep(3) | now() |
    +---------------------+----------+---------------------+
    | 2008-08-08 22:28:21 | 0 | 2008-08-08 22:28:21 |
    +---------------------+----------+---------------------+

    sysdate() 日期时间函数,一般情况下很少用到。

    MySQL 获得当前时间戳函数:current_timestamp, current_timestamp()

    mysql> select current_timestamp, current_timestamp();
    
    +---------------------+---------------------+
    | current_timestamp | current_timestamp() |
    +---------------------+---------------------+
    | 2008-08-09 23:22:24 | 2008-08-09 23:22:24 |
    +---------------------+---------------------+

     

    MySQL 日期转换函数、时间转换函数

    MySQL Date/Time to Str(日期/时间转换为字符串)函数:date_format(date,format), time_format(time,format)

    mysql> select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s');
    
    +----------------------------------------------------+
    | date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s') |
    +----------------------------------------------------+
    | 20080808222301 |
    +----------------------------------------------------+

    MySQL 日期、时间转换函数:date_format(date,format), time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式。它是 str_to_date(str,format) 函数的 一个逆转换。

    MySQL Str to Date (字符串转换为日期)函数:str_to_date(str, format)

    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

    可以看到,str_to_date(str,format) 转换函数,可以把一些杂乱无章的字符串转换为日期格式。另外,它也可以转换为时间。“format” 可以参看 MySQL 手册。

    %W 星期名字(Sunday……Saturday)  
    %D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)  
    %Y 年, 数字, 4 位  
    %y 年, 数字, 2 位  
    %a 缩写的星期名字(Sun……Sat)  
    %d 月份中的天数, 数字(00……31)  
    %e 月份中的天数, 数字(0……31)  
    %m 月, 数字(01……12)  
    %c 月, 数字(1……12)  
    %b 缩写的月份名字(Jan……Dec)  
    %j 一年中的天数(001……366)  
    %H 小时(00……23)  
    %k 小时(0……23)  
    %h 小时(01……12)  
    %I 小时(01……12)  
    %l 小时(1……12)  
    %i 分钟, 数字(00……59)  
    %r 时间,12 小时(hh:mm:ss [AP]M)  
    %T 时间,24 小时(hh:mm:ss)  
    %S 秒(00……59)  
    %s 秒(00……59)  
    %p AM或PM  
    %w 一个星期中的天数(0=Sunday ……6=Saturday )  
    %U 星期(0……52), 这里星期天是星期的第一天  
    %u 星期(0……52), 这里星期一是星期的第一天  
    %% 一个文字“%”。  

    MySQL (日期、天数)转换函数:to_days(date), from_days(days)

    select to_days('0000-00-00'); -- 0
    select to_days('2008-08-08'); -- 733627
    FROM_DAYS(N)  给出一个天数N,返回一个DATE值。  
    mysql> select FROM_DAYS(729669);  
    -> '1997-10-07'  

      

    MySQL (时间、秒)转换函数:time_to_sec(time), sec_to_time(seconds)

    select time_to_sec('01:00:05'); -- 3605
    select sec_to_time(3605); -- '01:00:05'

     

    查询数据库的日期转换:

    to_char(t.kssycdsj,'yyyy-mm-dd hh24:mi')
    to_date(#beginTime#,'yyyy-mm-dd hh24:mi')

    MySQL 拼凑日期、时间函数:makdedate(year,dayofyear), maketime(hour,minute,second)

    select makedate(2001,31); -- '2001-01-31'
    select makedate(2001,32); -- '2001-02-01'
    select maketime(12,15,30); -- '12:15:30'
  • 相关阅读:
    python操作MySQL数据库
    用python监控您的window服务
    关于position定位中的几个注意点
    filter 滤镜
    git使用心得
    :after,:before,content
    outline和border
    《css揭秘》之背景与边框
    css权威指南学习笔记--第6章
    浅谈setTimeout和setInterval
  • 原文地址:https://www.cnblogs.com/keyi/p/6385179.html
Copyright © 2020-2023  润新知