• 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);
  • 相关阅读:
    C/C++优秀书籍清单
    C语言内存管理详解
    C/C++语言常用头文件及函数
    如何学好C++语言
    VS2010 更改MFC标题及标题栏图标和exe图标
    Vue 实现前进刷新,后退不刷新的效果 玩转vue-router里的meta
    $nextTick 页面局部刷新 延迟加载
    吸顶动画 侧边栏 监听滚动条位置 监听元素距离顶部高度
    vue 自定义侧边栏 递归无限子级菜单
    vue element NavMenu 莫名出现蓝色边框
  • 原文地址:https://www.cnblogs.com/lxj-dream/p/8670643.html
Copyright © 2020-2023  润新知