在 MySql 中经常会用到日期,关于常用的日期函数,做了以下的总结:

1 . now()

作用; 获取当前的日期

除此之外,获取当前日期的函数还有: current_timestamp(); current_time; localtime(); localtime; localtimestamp(); localtimestamp;但是这些日期函数,与 now() 的效果相同, 为了方便记忆,建议使用 now() 来代替上面的函数。

  1. mysql> select now();
  2. +---------------------+
  3. | now() |
  4. +---------------------+
  5. | -- :: |
  6. +---------------------+
  7. row in set
  8.  
  9. mysql> select datediff(now(),'2015-1-1');
  10. +----------------------------+
  11. | datediff(now(),'2015-1-1') |
  12. +----------------------------+
  13. | |
  14. +----------------------------+
  15. row in set
  16.  
  17. mysql> select localtime();
  18. +---------------------+
  19. | localtime() |
  20. +---------------------+
  21. | -- :: |
  22. +---------------------+
  23. row in set
  24.  
  25. mysql> select current_timestamp();
  26. +---------------------+
  27. | current_timestamp() |
  28. +---------------------+
  29. | -- :: |
  30. +---------------------+
  31. row in set
  32.  
  33. mysql> select localtime;
  34. +---------------------+
  35. | localtime |
  36. +---------------------+
  37. | -- :: |
  38. +---------------------+
  39. row in set
  40.  
  41. mysql> select localtimestamp;
  42. +---------------------+
  43. | localtimestamp |
  44. +---------------------+
  45. | -- :: |
  46. +---------------------+
  47. row in set
  48.  
  49. mysql> select localtimestamp();
  50. +---------------------+
  51. | localtimestamp() |
  52. +---------------------+
  53. | -- :: |
  54. +---------------------+
  55. row in set
  56.  
  57. mysql> select sysdate();
  58. +---------------------+
  59. | sysdate() |
  60. +---------------------+
  61. | -- :: |
  62. +---------------------+
  63. row in set

2. sysdate()

作用: 这个日期函数与 now() 类似,不同的地方是:  now() 在执行开始时就得到的确定,而 sysdate() 则是在运行时动态得到 值。

如下所示:

  1. mysql> select now(), sleep(30), now();
  2. +---------------------+-----------+---------------------+
  3. | now() | sleep(30) | now() |
  4. +---------------------+-----------+---------------------+
  5. | 2015-05-13 15:01:49 | 0 | 2015-05-13 15:01:49 |
  6. +---------------------+-----------+---------------------+
  7. 1 row in set
  8.  
  9. mysql> select sysdate(), sleep(30), sysdate();
  10. +---------------------+-----------+---------------------+
  11. | sysdate() | sleep(30) | sysdate() |
  12. +---------------------+-----------+---------------------+
  13. | 2015-05-13 15:02:52 | 0 | 2015-05-13 15:03:22 |
  14. +---------------------+-----------+---------------------+
  15. 1 row in set

在用 now() 时,在途中 sleep 了 30秒 ,但是 Now() 的结果是相同的, 而在 sysdate() 中 sleep 了 30秒, 结果就不同了,结果是 sleep 30 秒后的值

3.  curdate()

作用; 获取当前日期

函数 current_date(); current_date; 与 curdate() 功能一样

  1. mysql> select curdate();
  2. +------------+
  3. | curdate() |
  4. +------------+
  5. | 2015-05-13 |
  6. +------------+
  7. 1 row in set
  8.  
  9. mysql> select current_date();
    +----------------+
    | current_date() |
    +----------------+
    | 2015-05-13     |
    +----------------+
    1 row in set
  10.  
  11. mysql> select current_date;
    +--------------+
    | current_date |
    +--------------+
    | 2015-05-13   |
    +--------------+
    1 row in set

4. curtime()

作用: 获取当前时间 ,其功能与 current_time current_time() 是一样的

  1. mysql> select curtime();
  2. +-----------+
  3. | curtime() |
  4. +-----------+
  5. | 15:04:42 |
  6. +-----------+
  7. 1 row in set
  8.  
  9. mysql> select current_time();
  10. +----------------+
  11. | current_time() |
  12. +----------------+
  13. | 15:05:05 |
  14. +----------------+
  15. 1 row in set
  16.  
  17. mysql> select current_time;
  18. +--------------+
  19. | current_time |
  20. +--------------+
  21. | 15:05:09 |
  22. +--------------+
  23. 1 row in set

5. utc_date(); utc_time(); utc_timestamp();

作用: 获取当前 utc 时间的函数

  1. mysql> select utc_timestamp(), utc_date(),utc_time(),now();
  2. +---------------------+------------+------------+---------------------+
  3. | utc_timestamp() | utc_date() | utc_time() | now() |
  4. +---------------------+------------+------------+---------------------+
  5. | 2015-05-13 07:05:51 | 2015-05-13 | 07:05:51 | 2015-05-13 15:05:51 |
  6. +---------------------+------------+------------+---------------------+
  7. 1 row in set

6. date(); year(); month(); day(); time(); week(); hour(); minute(); second(); microsecond();

作用; 获取日期中的部分值

  1. mysql> set @date='2015-05-05 14:23:34.345687'
  2. -> select date(@date)
  3. -> select time(@date)
  4. -> select year(@date)
  5. -> select quarter(@date)
  6. -> select month(@date)
  7. -> select week(@date)
  8. -> select day(@date)
  9. -> select hour(@date)
  10. -> select minute(@date)
  11. -> select second(@date)
  12. -> select microsecond(@date);

7. extract()

作用: 和上面所列举的函数功能一样, 只是函数的书写方式不同

  1. mysql> set @date='2015-05-05 14:23:34.345687'
  2. -> select extract(year from @date)

8.  last_day()

作用: 返回月份的最后一天

9. datedifff()

作用: 计算两个日期间的相差的天数

  1. mysql> select datediff(now(),'2015-05-05');
  2. +------------------------------+
  3. | datediff(now(),'2015-05-05') |
  4. +------------------------------+
  5. | 8 |
  6. +------------------------------+

10. str_to_date(str,format)

作用: 可以把一些杂乱无章的字符串转换成日期格式,也可以转换成时间

有关更多的 Mysql 函数,可以参考 Mysql 在线手册:http://www.cbi.pku.edu.cn/chinese/documents/csdoc/mysql/manual_toc.html

MySql 日期函数的更多相关文章

  1. [php基础]Mysql日期函数:日期时间格式转换函数详解

    在PHP网站开发中,Mysql数据库设计中日期时间字段必不可少,由于Mysql日期函数输出的日期格式与PHP日期函数之间的日期格式兼容性不够,这就需要根据网站实际情况使用Mysql或PHP日期转换函数 ...

  2. mysql 日期函数总结

    1.0 格式化:DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 语法 DATE_FORMAT(date,format) date 参数是合法的日期.format 规定日期/时间的 ...

  3. MYSQL 日期函数【转】

    MySQL日期时间函数大全 DAYOFWEEK(date) 返回日期date是星期几(=星期六,ODBC标准) mysql> select DAYOFWEEK('1998-02-03'); WE ...

  4. mysql日期函数(转)

    MySQL 获得当前日期时间 函数 获得当前日期+时间(date + time)函数:now() mysql> select now(); +---------------------+ | n ...

  5. Mysql日期函数,时间函数使用的总结

    一.MySQL 获得当前日期时间 函数 1.1 获得当前日期+时间(date + time)函数:now() mysql> select now();+--------------------- ...

  6. MySQL:日期函数、时间函数总结

    MySQL 获得当前日期时间 函数 查询昨天,时间拼接 select concat(DATE_FORMAT(date_add(now(), interval -1 day),'%Y-%d-%d'),& ...

  7. MySQL日期函数、时间函数总结(MySQL 5.X)

    一.获得当前日期时间函数 1.1 获得当前日期+时间(date + time)函数:now() select now(); # :: 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下 ...

  8. mysql 日期函数大全

    对于每个类型拥有的值范围以及并且指定日期何时间值的有效格式的描述见7.3.6 日期和时间类型. 这里是一个使用日期函数的例子.下面的查询选择了所有记录,其date_col的值是在最后30天以内: my ...

  9. MySQL日期函数与日期转换格式化函数大全

    Mysql作为一款开元的免费关系型数据库,用户基础非常庞大,本文列出了MYSQL常用日期函数与日期转换格式化函数 1.DAYOFWEEK(date) 1 2 SELECT DAYOFWEEK('201 ...

随机推荐

  1. linux 鼠标中键粘帖功能?!!

    转载自:http://yjhexy.iteye.com/blog/785564 ubuntu鼠标中键问题,其实也不是什么问题,ubuntu的鼠标中键是用来快速粘贴的,只是windows用惯了,时不时手 ...

  2. 一. Logback与p6spy

    一. LogBack配置 配置pom.xml <dependency> <groupId>org.slf4j</groupId> <artifactId> ...

  3. CF 369C . Valera and Elections tree dfs 好题

    C. Valera and Elections   The city Valera lives in is going to hold elections to the city Parliament ...

  4. python(6)时间戳和北京时间互转,输出当前的时间和推到七天前的日期

    项目发展的需要:(包含时间函数)time datetime 时间戳和北京时间互转 import time import datetime s = '2015-04-17 11:25:30' d = d ...

  5. Java System.out的输出缓冲

    今天学习了java的正则表达式api,在写例子的时候遇到了让人摸不着头脑的问题:从控制台输入了字符串,却没有输出;直到输入的字符串不能匹配的时,一起与Unabled to match输出.相关代码如下 ...

  6. nginx优化配置

    一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1.  worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu ...

  7. Mac 10.10下安装MySQL5.6.21提示安装失败

    只要要在安装的第三步在自定里不要选Startup item就可以了

  8. Chrome每次打開都要打開123.sogou.com

    剛開始還以為中毒了,又是殺毒又是掃描的,最後發覺,原來就是chrome的一個設置被改了. Chrome->設置->啟動時 : 選打开特定网页或一组网页->設置網頁 , 將其中的123 ...

  9. ndk的一些概念

    什么场景应用ndk 1.代码的包含,apk的java层代码容易被反编译,c/c++被反编译难度非常大 2.NDK中调用 第三方C/C++库,因为大部分的开源库都是c/c++编写,比如opencv,op ...

  10. html5 基本内容 摘自W3C

    HTML5 教程(摘录自 W3C School) HTML 5 简介(HTML5 是下一代的 HTML) 什么是 HTML5? HTML5 将成为 HTML.XHTML 以及 HTML DOM 的新标 ...