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

1 . now()

作用; 获取当前的日期

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

mysql> select now();
+---------------------+
| now() |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select datediff(now(),'2015-1-1');
+----------------------------+
| datediff(now(),'2015-1-1') |
+----------------------------+
| |
+----------------------------+
row in set mysql> select localtime();
+---------------------+
| localtime() |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select localtime;
+---------------------+
| localtime |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select localtimestamp;
+---------------------+
| localtimestamp |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select localtimestamp();
+---------------------+
| localtimestamp() |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select sysdate();
+---------------------+
| sysdate() |
+---------------------+
| -- :: |
+---------------------+
row in set

2. sysdate()

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

如下所示:

mysql> select now(), sleep(30), now();
+---------------------+-----------+---------------------+
| now() | sleep(30) | now() |
+---------------------+-----------+---------------------+
| 2015-05-13 15:01:49 | 0 | 2015-05-13 15:01:49 |
+---------------------+-----------+---------------------+
1 row in set mysql> select sysdate(), sleep(30), sysdate();
+---------------------+-----------+---------------------+
| sysdate() | sleep(30) | sysdate() |
+---------------------+-----------+---------------------+
| 2015-05-13 15:02:52 | 0 | 2015-05-13 15:03:22 |
+---------------------+-----------+---------------------+
1 row in set

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

3.  curdate()

作用; 获取当前日期

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

mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2015-05-13 |
+------------+
1 row in set mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2015-05-13     |
+----------------+
1 row in set mysql> select current_date;
+--------------+
| current_date |
+--------------+
| 2015-05-13   |
+--------------+
1 row in set

4. curtime()

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

mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 15:04:42 |
+-----------+
1 row in set mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 15:05:05 |
+----------------+
1 row in set mysql> select current_time;
+--------------+
| current_time |
+--------------+
| 15:05:09 |
+--------------+
1 row in set

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

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

mysql> select utc_timestamp(), utc_date(),utc_time(),now();
+---------------------+------------+------------+---------------------+
| utc_timestamp() | utc_date() | utc_time() | now() |
+---------------------+------------+------------+---------------------+
| 2015-05-13 07:05:51 | 2015-05-13 | 07:05:51 | 2015-05-13 15:05:51 |
+---------------------+------------+------------+---------------------+
1 row in set

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

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

mysql> set @date='2015-05-05 14:23:34.345687'
-> select date(@date)
-> select time(@date)
-> select year(@date)
-> select quarter(@date)
-> select month(@date)
-> select week(@date)
-> select day(@date)
-> select hour(@date)
-> select minute(@date)
-> select second(@date)
-> select microsecond(@date);

7. extract()

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

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

8.  last_day()

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

9. datedifff()

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

mysql> select datediff(now(),'2015-05-05');
+------------------------------+
| datediff(now(),'2015-05-05') |
+------------------------------+
| 8 |
+------------------------------+

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. Configure custom SSL certificate for RDP on Windows Server 2012 in Remote Administration mode

    Q: So the release of Windows Server 2012 has removed a lot of the old Remote Desktop related configu ...

  2. QT显示中文的几个问题

    最近用QT,需要在界面上显示中文,发现QT无法直接在代码中写中文,只能通过曲线救国的方式,比如用QT语言家,QTextCodec的fromloca8bit 研究了半天,终于明白了一些编码的问题 1.V ...

  3. 无法找到类:java.lang.ClassNotFoundException: com.tt.javaweb.HttpServletRequest问题解决

    问题如下:找不到Httpservlet对应的Class,进入build目录下发现确实没有对应的class文件. 严重: Allocate exception for HttpServletReques ...

  4. windows7使用Source insight上远程修改ubuntu共享内核源码

    由于本人阅读喜欢使用source insight.前段时间接触了linux核代码,而这份代码只能放在ubuntu服务器上编译,刚开始的时候是在windows上修改,完了之后再copy到服务器上去编译, ...

  5. (WPF) MVVM: 动态添加控件及绑定。

    比如需要显示一个键盘,里面有各个按键.实现的效果如下: 之前的思路,就是建立一个singleKey的控件,然后在后台用代码动态的添加到父控件里去, 再用代码在后台进行绑定. 这种实现方法并不是真正的M ...

  6. c++builder6.0 mdi窗体+自定义子窗体

  7. REST总结(转)

    REST总结           REST(Representational State Transfer)是代表状态传输的缩写,它代表了分布式超媒体系统的体系结构风格,它是一种针对网络应用的设计和开 ...

  8. Spring MVC 中文乱码的解决

    对于POST方法提交的中文乱码 , 可在web.xml中添加如下代码 : <filter> <filter-name>encodingFilter</filter-nam ...

  9. 关于Switch结构利用

    三大流程结构,循环.分支.if ,循环与条件选择结构用的比较多,而swicth用的比较少,swicth可以用if代替,只不过麻烦,最终都能实现输入和输出的条件对应     Swicth利用       ...

  10. jq鼠标点击滚动锚点

    鼠标点击滚动锚点 //滚动锚点 $('.menus-c ul li a').click(function(){ //alert(); $('html, body').animate({ scrollT ...