zoukankan      html  css  js  c++  java
  • MySQL日期/时间函数

    1、查询当前时间函数:

    select NOW(),LOCALTIME(),SYSDATE(),CURRENT_TIMESTAMP();

    但是now()与sysdate()有点差异的,一个语句中now()的值是不变的,而sysdate()是动态获取的,例如

    select NOW(),SLEEP(2),NOW();
    SELECT SYSDATE(),SLEEP(2),SYSDATE();

    -------通过在MySQL中执行select sleep(N)可以让此语句运行N秒钟

    2、获取当前日期,curdate()=current_date()=current_date

    select curdate(),current_date(),current_date;

    3、获取当前时间,curtime()=current_time()=current_time

    select curtime(),current_time(),current_time;

    4、获取年,月,日

    select year(now()),month(now()),day(now());

    5、获取当前星期几,几月,以英文返回,dayname(),monthname()

    select dayname(curdate()),monthname(curdate());

    6、获取某个日期在周,月,年中的位置,dayofweek(),dayofmonth,dayofyear(),如要返回中文周几,可以在程序中进行逻辑

    select dayofweek(now()),dayofmonth(now()),dayofyear(now());

    7、获取一个月的最后一天,last_day(),利用它可以得到某个月有多少天

    select last_day(NOW()),day(last_day(NOW()));

    8、获取某天位于一年中的第N周week(date,3)=weekofyear(),week()函数的第二个参数用来设定以星期几做为一周的开始

    select week(now(),3),weekofyear(now());

    9、获取两个日期或者两个时间的相差,datediff(),timediff()

    select datediff(curdate(),'2018-02-15'),timediff(curtime(),'09:09:09')

    10、时间与秒的转换,time_to_sec(),sec_to_time()

    select time_to_sec(@d),sec_to_time(12389);

    11、日期与天数的转换,to_days(),from_days()

    12、字符串转换为日期,str_to_date(date,format)

    select str_to_date('09/09/20','%Y/%m/%d');
    select str_to_date('09.09.20','%Y.%m.%d');

    13、日期/时间拼凑,makedate(year,dayofyear),maketime(hour,minute,second)

    select makedate(2015,200),maketime(13,20,15);
  • 相关阅读:
    Git 最新版本2.20.1 安装及配置
    Python中:ModuleNotFoundError: No module named 'alipay'
    Python调用time模块设置当前时间-指定时间
    python 正则表达式
    pwntools出现的一些问题
    strace监视系统调用
    深入理解Linux内核 学习笔记(8)
    ubuntu 查看版本
    LSTM实现中文文本情感分析
    深入理解Linux内核 学习笔记(5)
  • 原文地址:https://www.cnblogs.com/lxj-dream/p/8670643.html
Copyright © 2011-2022 走看看