2. 获得当前日期(date)函数:curdate()
mysql> select curdate(); +----------------+ | curdate() | +----------------+ | 2014-10-29 | +----------------+
其中,下面的两个日期函数等同于 curdate():
current_date() current_date
3. 获得当前时间(time)函数:curtime()
mysql> select curtime(); +-----------+ | curtime() | +-----------+ | 10:59:58 | +-----------+
其中,下面的两个时间函数等同于 curtime():
current_time() current_time
4. 获得当前 UTC 日期时间函数:utc_date(), utc_time(), utc_timestamp() 、
mysql> select utc_timestamp(), utc_date(), utc_time(), now() ; +-------------------------------+-----------------+-----------------+-------------------------------+ | utc_timestamp() | utc_date() | utc_time() | now() | +-------------------------------+-----------------+-----------------+-------------------------------+ | 2014-10-29 03:00:55 | 2014-10-29 | 03:00:55 | 2014-10-29 11:00:55 | +-------------------------------+-----------------+-----------------+-------------------------------+
因为我国位于东八时区,所以本地时间 = UTC 时间 + 8 小时。UTC 时间在业务涉及多个国家和地区的时候,非常有用。
5、MySQL 日期时间 Extract(选取) 函数。
5.1. 选取日期时间的各个部分:日期、时间、年、季度、月、日、小时、分钟、秒、微秒
set @dt = '2008-09-10 07:15:30.123456'; select date(@dt); -- 2008-09-10 select time(@dt); -- 07:15:30.123456 select year(@dt); -- 2008 select quarter(@dt); -- 3 select month(@dt); -- 9 select week(@dt); -- 36 select day(@dt); -- 10 select hour(@dt); -- 7 select minute(@dt); -- 15 select second(@dt); -- 30 select microsecond(@dt); -- 123456 5.2. MySQL Extract() 函数,可以上面实现类似的功能:
set @dt = '2008-09-10 07:15:30.123456'; select extract(year from @dt); -- 2008 select extract(quarter from @dt); -- 3 select extract(month from @dt); -- 9 select extract(week from @dt); -- 36 select extract(day from @dt); -- 10 select extract(hour from @dt); -- 7 select extract(minute from @dt); -- 15 select extract(second from @dt); -- 30 select extract(microsecond from @dt); -- 123456 select extract(year_month from @dt); -- 200809 select extract(day_hour from @dt); -- 1007 select extract(day_minute from @dt); -- 100715 select extract(day_second from @dt); -- 10071530 select extract(day_microsecond from @dt); -- 10071530123456 select extract(hour_minute from @dt); -- 715 select extract(hour_second from @dt); -- 71530 select extract(hour_microsecond from @dt); -- 71530123456 select extract(minute_second from @dt); -- 1530 select extract(minute_microsecond from @dt); -- 1530123456 select extract(second_microsecond from @dt); -- 30123456
MySQL Extract() 函数唯一不好的地方在于:你需要多敲几次键盘。
6. MySQL dayof... 函数:dayofweek(), dayofmonth(), dayofyear()
分别返回日期参数,在一周、一月、一年中的位置。set @dt = '2008-08-08'; select dayofweek(@dt); -- 6 select dayofmonth(@dt); -- 8 select dayofyear(@dt); -- 221日期 '2008-08-08' 是一周中的第 6 天(1 = Sunday, 2 = Monday, ..., 7 = Saturday);一月中的第 8 天;一年中的第 221 天。
7. MySQL week... 函数:week(), weekofyear(), dayofweek(), weekday(), yearweek()
set @dt = '2008-08-08'; select week(@dt); -- 31 select week(@dt,3); -- 32 select weekofyear(@dt); -- 32 select dayofweek(@dt); -- 6 select weekday(@dt); -- 4 select yearweek(@dt); -- 200831
MySQL