MySQL日期时间的处理,在其官网文档上都有详细的阐述,想了解更多的同学可自行查阅。

1.查询当前日期时间:函数有now(),localtime(),current_timestamp(),sysdate()。

mysql> select now(),localtime(),current_timestamp(),sysdate();
+---------------------+---------------------+---------------------+---------------------+
| now() | localtime() | current_timestamp() | sysdate() |
+---------------------+---------------------+---------------------+---------------------+
| 2015-04-16 09:29:18 | 2015-04-16 09:29:18 | 2015-04-16 09:29:18 | 2015-04-16 09:29:18 |
+---------------------+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)

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

mysql> select now(),sleep(2),now();
+---------------------+----------+---------------------+
| now() | sleep(2) | now() |
+---------------------+----------+---------------------+
| 2015-04-16 09:34:30 | 0 | 2015-04-16 09:34:30 |
+---------------------+----------+---------------------+
1 row in set (2.00 sec) mysql> select sysdate(),sleep(2),sysdate();
+---------------------+----------+---------------------+
| sysdate() | sleep(2) | sysdate() |
+---------------------+----------+---------------------+
| 2015-04-16 09:35:15 | 0 | 2015-04-16 09:35:17 |
+---------------------+----------+---------------------+
1 row in set (2.00 sec) -----有此结果可以看出,一般在生成环境中最好使用now(),当然也可以使用sysdate-is-now参数改变sysdate()的行为和now()一样------

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

mysql> select curdate(),current_date(),current_date;
+------------+----------------+--------------+
| curdate() | current_date() | current_date |
+------------+----------------+--------------+
| 2015-04-16 | 2015-04-16 | 2015-04-16 |
+------------+----------------+--------------+
1 row in set (0.00 sec)

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

mysql> select curtime(),current_time(),current_time;
+-----------+----------------+--------------+
| curtime() | current_time() | current_time |
+-----------+----------------+--------------+
| 09:42:17 | 09:42:17 | 09:42:17 |
+-----------+----------------+--------------+
1 row in set (0.00 sec)

4.获取UTC日期时间,utc_date(),utc_time(),utc_timestamp(),中国属于东八区,故+8小时即可

mysql> select utc_date(),utc_time(),utc_timestamp();
+------------+------------+---------------------+
| utc_date() | utc_time() | utc_timestamp() |
+------------+------------+---------------------+
| 2015-04-16 | 01:55:23 | 2015-04-16 01:55:23 |
+------------+------------+---------------------+
1 row in set (0.00 sec)

5.获取年,月,日

mysql> select year(now()),month(now()),day(now());
+-------------+--------------+------------+
| year(now()) | month(now()) | day(now()) |
+-------------+--------------+------------+
| 2015 | 4 | 16 |
+-------------+--------------+------------+
1 row in set (0.00 sec)

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

mysql> select dayname(curdate()),monthname(curdate());
+--------------------+----------------------+
| dayname(curdate()) | monthname(curdate()) |
+--------------------+----------------------+
| Thursday | April |
+--------------------+----------------------+
1 row in set (0.03 sec)

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

mysql> set @d=now();
Query OK, 0 rows affected (0.03 sec) mysql> select dayofweek(@d),dayofmonth(@d),dayofyear(@d);
+---------------+----------------+---------------+
| dayofweek(@d) | dayofmonth(@d) | dayofyear(@d) |
+---------------+----------------+---------------+
| 5 | 16 | 106 |
+---------------+----------------+---------------+
1 row in set (0.03 sec)

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

mysql> select last_day(@d),day(last_day(@d));
+--------------+-------------------+
| last_day(@d) | day(last_day(@d)) |
+--------------+-------------------+
| 2015-04-30 | 30 |
+--------------+-------------------+
1 row in set (0.00 sec)

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

mysql> select week(@d,3),weekofyear(@d);
+------------+----------------+
| week(@d,3) | weekofyear(@d) |
+------------+----------------+
| 16 | 16 |
+------------+----------------+
1 row in set (0.00 sec)

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

mysql> select datediff(curdate(),'2015-02-15'),timediff(curtime(),'09:09:09')
+----------------------------------+--------------------------------+
| datediff(curdate(),'2015-02-15') | timediff(curtime(),'09:09:09') |
+----------------------------------+--------------------------------+
| 60 | 01:21:20 |
+----------------------------------+--------------------------------+
1 row in set (0.05 sec)

11.为日期加上或减去一个间隔,date_add(),date_sub()

mysql> select date_add(curdate(),interval 1 day),date_sub(curdate(),interval 1 day);
+------------------------------------+------------------------------------+
| date_add(curdate(),interval 1 day) | date_sub(curdate(),interval 1 day) |
+------------------------------------+------------------------------------+
| 2015-04-17 | 2015-04-15 |
+------------------------------------+------------------------------------+
1 row in set (0.03 sec) mysql> select date_add(@d,interval '01:15:09' hour_second),date_sub(@d,interval '01:15:09' hour_second);
+----------------------------------------------+----------------------------------------------+
| date_add(@d,interval '01:15:09' hour_second) | date_sub(@d,interval '01:15:09' hour_second) |
+----------------------------------------------+----------------------------------------------+
| 2015-04-16 11:21:42 | 2015-04-16 08:51:24 |
+----------------------------------------------+----------------------------------------------+
1 row in set (0.00 sec)

12.时间与秒的转换,time_to_sec(),sec_to_time()

mysql> select time_to_sec(@d),sec_to_time(12389);
+-----------------+--------------------+
| time_to_sec(@d) | sec_to_time(12389) |
+-----------------+--------------------+
| 36393 | 03:26:29 |
+-----------------+--------------------+
1 row in set (0.00 sec)

13.日期与天数的转换,to_days(),from_days()

mysql> select to_days(@d),from_days(1460000);
+-------------+--------------------+
| to_days(@d) | from_days(1460000) |
+-------------+--------------------+
| 736069 | 3997-05-06 |
+-------------+--------------------+
1 row in set (0.00 sec)

14.字符串转换为日期,str_to_date(date,format)

mysql> select str_to_date('09/09/20','%Y/%m/%d');
+------------------------------------+
| str_to_date('09/09/20','%Y/%m/%d') |
+------------------------------------+
| 2009-09-20 |
+------------------------------------+
1 row in set (0.00 sec) mysql> select str_to_date('09.09.20','%Y.%m.%d');
+------------------------------------+
| str_to_date('09.09.20','%Y.%m.%d') |
+------------------------------------+
| 2009-09-20 |
+------------------------------------+
1 row in set (0.00 sec)

format标志含义:

%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%c Month, numeric (0..12)
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00..53), where Sunday is the first day of the week
%u Week (00..53), where Monday is the first day of the week
%V Week (01..53), where Sunday is the first day of the week; used with %X
%v Week (01..53), where Monday is the first day of the week; used with %x
%W Weekday name (Sunday..Saturday)
%w Day of the week (0=Sunday..6=Saturday)
%X Year for the week where Sunday is the first day of the week, numeric, four digits;
used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four
digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal “%” character
%x x, for any “x” not listed above

15.日期格式化,date_format(str,format)

mysql> select date_format('09.09.20','%Y:%m:%d');
+------------------------------------+
| date_format('09.09.20','%Y:%m:%d') |
+------------------------------------+
| 2009:09:20 |
+------------------------------------+
1 row in set (0.00 sec)

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

mysql> select makedate(2015,200),maketime(13,20,15);
+--------------------+--------------------+
| makedate(2015,200) | maketime(13,20,15) |
+--------------------+--------------------+
| 2015-07-19 | 13:20:15 |
+--------------------+--------------------+
1 row in set (0.00 sec)

17.unix时间戳,时间戳转换等,unix_timestamp(date),timestamp(date)

mysql> select unix_timestamp(),unix_timestamp('2009-09-09'),timestamp(now());
+------------------+------------------------------+---------------------+
| unix_timestamp() | unix_timestamp('2009-09-09') | timestamp(now()) |
+------------------+------------------------------+---------------------+
| 1429153960 | 1252425600 | 2015-04-16 11:12:40 |
+------------------+------------------------------+---------------------+
1 row in set (0.00 sec)

18.在应用中碰到需要比较日期时,比如获取某一天的数据,不能使用“=”等于号来比较,使用"<>",比如

select * from users where regDate<'2009-10-23' and regDate>='2009-10-22' //如果使用regDate='2009-10-22'相当于00:00:00

19.知道用户的生日得到年龄

mysql> select year(curdate())-year('1990-07-30')-(right(curdate(),5)<right('1990-07-30',5)) as age;
+------+
| age |
+------+
| 24 |
+------+
1 row in set (0.05 sec)

MySQL学习笔记八:日期/时间的处理的更多相关文章

  1. MySQL学习笔记:生成时间维度表

    # ---- 对应时间戳怎么生成的? ---- /*TIME_CD TIME_CD1000000 000005000001 000005000002 000005000003 000005000004 ...

  2. PL/SQL学习笔记之日期时间

    一:PL/SQL时间相关类型 PL/SQL提供两个和日期时间相关的数据类型: 日期时间(Datetime)数据类型 时间间隔类型 二:日期时间类型 datetime数据类型有: DATE TIMEST ...

  3. php 学习笔记之日期时间操作一箩筐

    格式化日期时间 date : 格式化日期时间 场景 将当前日期时间或者特定日期时间格式化输出为特定格式的字符串,常用于人性化展示信息. 说明 返回给定时间戳格式化后所产生的日期时间字符串,如果没有给出 ...

  4. ANDROID_MARS学习笔记_S01_010日期时间控件

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  5. MySQL学习笔记:生成时间维度表2

    实现目的: 测试: # 测试 加一秒 SECOND), INTERVAL SECOND); SECOND),'%H%i%s');# 第一秒 SECOND),'%H%i%s');# 最后一秒 SELEC ...

  6. MYSQL学习笔记三:日期和时间函数

    MYSQL学习笔记三:日期和时间函数 1. 获取当前日期的函数和获取当前时间的函数 /*获取当前日期的函数和获取当前时间的函数.将日期以'YYYY-MM-DD'或者'YYYYMMDD'格式返回 */ ...

  7. Mysql学习笔记(一)数据类型

    原文:Mysql学习笔记(一)数据类型 学习内容: Mysql基本数据类型. 1.数字类型.. i.整型     Mysql数据类型             含义(有符号)     tinyint(m ...

  8. MySql学习笔记四

    MySql学习笔记四 5.3.数据类型 数值型 整型 小数 定点数 浮点数 字符型 较短的文本:char, varchar 较长的文本:text, blob(较长的二进制数据) 日期型 原则:所选择类 ...

  9. MySQL学习笔记一

    MySQL 学习笔记 一 一.数据库简单介绍 1. 按照数据库的发展时间顺序,主要出现了以下类型数据库系统: Ø 网状型数据库 Ø 层次型数据库 Ø 关系型数据库 Ø 面向对象数据库 上面4中数据库系 ...

随机推荐

  1. bootstrap内置网格式布局系统:

    bootstrap分为12栏,若想要一个元素占用一定的栏数的宽度,可以在这个元素上用一个特定的类,就比如说span1.span2....类. 定义的布局: 定义page-header类,在这个类当中为 ...

  2. 简单谈谈如何利用h5实现音频的播放

    作者:白狼 出处:http://www.manks.top/article/h5_audio本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律 ...

  3. gulp-babel使用

    各大浏览器厂商对es2015功能支持不完全,等到全部支持会等很长时间,如果现在使用es2015,可以选择babel一个将ES6/ES7写的代码转换为ES5代码的编译器. 我们选择使用gulp自动化编译 ...

  4. Ubuntu Java 环境变量

    方法1:修改/etc/profile 文件所有用户的 shell都有权使用这些环境变量<1>在 shell终端执行命令:vi /etc/profile<2>在 profile文 ...

  5. jsp页面 如何通过el表达式获取request属性值

    1. 我在一个超连接后加个参数如:      http://localhost:8080/test/testjstl.jsp?pid=001    此时在jsp页面中,获取jsp传过来的pid的参数值 ...

  6. STM32之通用定时器

    广大的互联网的大家早上中午晚上..又好..没错了..我又来了..写博客不是定时的..为什么我要提写博客不是定时的呢??聪明的人又猜到我要说什么了吧.有前途.其实我还是第一次听到定时器有通用和高级之分的 ...

  7. Python之路【第三篇】python基础 之基本数据类型 补充

    字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...

  8. POJ1860 Currency Exchange(bellman-ford)

    链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...

  9. [译]MVC网站教程(一):多语言网站框架

    本文简介 本博文介绍了 Visual Studio 工具生成的 ASP.NET MVC3 站点的基本框架:怎样实现网站的语言的国际化与本地化功能,从零开始实现用户身份认证机制,从零开始实现用户注册机制 ...

  10. .Net组件程序设计之异步调用

    .Net组件程序设计之异步调用 说到异步调用,在脑海中首先想到就是BeginInvoke(),在一些常用对象中我们也会常常见到Invoke()和BeginInvoke(), 要想让自己的组件可以被客户 ...