zoukankan      html  css  js  c++  java
  • 【mysql时间类sql处理总结】

    获取当前时间:【年-月-日 时:分:秒】

    -- 格式:2020-05-15 19:57:47
    select now() from test -- 建议
    select current_timestamp from test
    select current_timestamp() from test
    select localtime from test
    select localtime() from test

    获取当前时间:【年-月-日】

    -- 格式:2020-05-15
    select curdate() from test -- 建议
    select current_date from test
    select current_date() from test

    获取当前时间:【时:分:秒】

    -- 格式:19:06:49
    select curtime() from test
    select current_time from test
    select current_time() from test

    获取月份中的最后一天:【年-月-日】

    -- 格式:2020-02-29
    select last_day('2020-02-01');

    获取当前月份有多少天:【天数】

    -- 格式:31
    select day(last_day(now())) as days;

    获取两个时间之间得天数:【天数】

    -- 格式 4
    select datediff('2020-05-05', '2020-05-01') from test;

    获取时间得差值:【时:分:秒】

    -- 格式:08:08:08
    select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00') from test;

    时间-字符串-时间戳之间的转换

    时间转字符串:

    -- 格式:2020-05-15 19:23:25
    select date_format(now(), '%Y-%m-%d %H:%i:%s');

    时间转时间戳:

    -- 格式:1589531039
    select unix_timestamp(now());

    时间戳转字符串:

    -- 格式:2018-05-02 20:16:23
    select from_unixtime(1525263383, '%Y-%m-%d %H:%i:%s');

    时间戳转时间:

    -- 2018-05-02 20:16:23
    select from_unixtime(1525263383);

    字符串转时间:

    -- 格式:2020-05-02 19:20:20
    select str_to_date('2020-05-02 19:20:20','%Y-%m-%d %H:%i:%s');

    字符串转时间戳:

    -- 1525263850
    select unix_timestamp('2018-05-02 20:24:10');

    参考:

    1. https://www.cnblogs.com/blueness-sunshine/p/4912058.html

    2. https://www.cnblogs.com/zuiyue_jing/p/12769486.html

    注意:本篇文章仅用于个人学习,如有侵权,联系删除!!!

    持续更新!!!

  • 相关阅读:
    使用IDEA启动Tomcat时出现端口被占用的问题解决办法
    在Navicat中设置id主键为UUID自增
    关于IDEA构建的maven项目:WEB-INF下的jsp移动到webapp下出现404无法访问的问题
    Spring框架学习日志(2/4)
    Spring框架学习日志(1/4)
    jenkins+python+pytest+selenium 自动化执行脚本并发送报告
    Selenium 添加Cookie实现绕过登录流程
    CSS
    集合
    Javaweb初学
  • 原文地址:https://www.cnblogs.com/flyinghome/p/12895546.html
Copyright © 2011-2022 走看看