MySQL日期函式與日期轉換格式化函式大全
阿新 • • 發佈:2019-01-05
文章目錄
- 獲得當前日期+時間(date + time)函式:now()
- 獲得當前日期+時間(date + time)函式:sysdate()
- 獲得當前日期(date)函式:curdate()
- 獲得當前時間(time)函式:curtime()
- 獲得當前 UTC 日期時間函式:utc_date(), utc_time(), utc_timestamp()
- 選取日期時間的各個部分:日期、時間、年、季度、月、日、小時、分鐘、秒、微秒
- dayof… 函式:dayofweek(), dayofmonth(), dayofyear() 分別返回日期引數,在一週、一月、一年中的位置
- 返回星期和月份名稱函式:dayname(), monthname()
- last_day() 函式:返回月份中的最後一天
- 日期增加一個時間間隔:date_add()
- 日期減去一個時間間隔:date_sub()
- 另類日期函式:period_add(P,N), period_diff(P1,P2)
- period_add(P,N)函式引數“P” 的格式為“YYYYMM” 或者 “YYMM”,第二個引數“N” 表示增加或減去 N month(月)
- period_diff(P1,P2):日期 P1-P2,返回 N 個月。
- 日期、時間相減函式:datediff(date1,date2), timediff(time1,time2)
- datediff(date1,date2):兩個日期相減 date1 – date2,返回天數
- timediff(time1,time2):兩個日期相減 time1 – time2,返回 time 差值
- 日期轉換函式、時間轉換函式
- (時間、秒)轉換函式:time_to_sec(time), sec_to_time(seconds)
- (日期、天數)轉換函式:to_days(date), from_days(days)
- str_to_date(字串轉換為日期)函式:str_to_date(str, format)
- MySQL中日期格式
- Date/Time to Str(日期/時間轉換為字串)函式:date_format(date,format), time_format(time,format)
- 獲得國家地區時間格式函式:get_format()
- 拼湊日期、時間函式:makdedate(year,dayofyear), maketime(hour,minute,second)
- 時間戳(Timestamp)函式
- 獲得當前時間戳函式:current_timestamp, current_timestamp()
- (Unix 時間戳、日期)轉換函式
- unix_timestamp()
- unix_timestamp(date)
- from_unixtime(unix_timestamp)
- from_unixtime(unix_timestamp,format)
- 時間戳(timestamp)轉換、增、減函式
- 時區(timezone)轉換函式convert_tz(dt,from_tz,to_tz)
獲得當前日期+時間(date + time)函式:now()
select now();
+---------------------+
| now() |
+---------------------+
| 2018-12-25 20:23:49 |
+---------------------+
1 row in set (0.00 sec)
除了 now() 函式能獲得當前的日期時間外,MySQL 中還有下面的函式:
current_timestamp()/current_timestamp
select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2018-12-25 20:26:47 |
+---------------------+
1 row in set (0.00 sec)
select current_timestamp;
+---------------------+
| current_timestamp |
+---------------------+
| 2018-12-25 20:28:42 |
+---------------------+
1 row in set (0.00 sec)
localtime()/localtime
select localtime();
+---------------------+
| localtime() |
+---------------------+
| 2018-12-25 20:29:57 |
+---------------------+
1 row in set (0.00 sec)
select localtime;
+---------------------+
| localtime |
+---------------------+
| 2018-12-25 20:30:08 |
+---------------------+
1 row in set (0.00 sec)
localtimestamp()/localtimestamp — (v4.0.6)
select localtimestamp();
+---------------------+
| localtimestamp() |
+---------------------+
| 2018-12-25 20:30:26 |
+---------------------+
1 row in set (0.00 sec)
select localtimestamp;
+---------------------+
| localtimestamp |
+---------------------+
| 2018-12-25 20:30:35 |
+---------------------+
1 row in set (0.00 sec)
這些日期時間函式,都等同於 now()。鑑於 now() 函式簡短易記,建議總是使用 now() 來替代上面列出的函式。
獲得當前日期+時間(date + time)函式:sysdate()
sysdate() 日期時間函式跟 now() 類似,不同之處在於:now() 在執行開始時值就得到了, sysdate() 在函式執行時動態得到值。看下面的例子就明白了:
select now(), sleep(3), now();
+---------------------+----------+---------------------+
| now() | sleep(3) | now() |
+---------------------+----------+---------------------+
| 2018-12-25 20:32:33 | 0 | 2018-12-25 20:32:33 |
+---------------------+----------+---------------------+
1 row in set (3.00 sec)
select sysdate(), sleep(3), sysdate();
+---------------------+----------+---------------------+
| sysdate() | sleep(3) | sysdate() |
+---------------------+----------+---------------------+
| 2018-12-25 20:33:14 | 0 | 2018-12-25 20:33:17 |
+---------------------+----------+---------------------+
1 row in set (3.00 sec)
可以看到,雖然中途 sleep 3 秒,但 now() 函式兩次的時間值是相同的; sysdate() 函式兩次得到的時間值相差 3 秒。MySQL Manual 中是這樣描述 sysdate() 的:Return the time at which the function executes。
sysdate() 日期時間函式,一般情況下很少用到。
獲得當前日期(date)函式:curdate()
select curdate();
+------------+
| curdate() |
+------------+
| 2018-12-25 |
+------------+
1 row in set (0.00 sec)
其中,下面的兩個日期函式等同於 curdate():
current_date()/current_date
select current_date();
+----------------+
| current_date() |
+----------------+
| 2018-12-25 |
+----------------+
1 row in set (0.00 sec)
select current_date;
+--------------+
| current_date |
+--------------+
| 2018-12-25 |
+--------------+
1 row in set (0.00 sec)
獲得當前時間(time)函式:curtime()
select curtime();
+-----------+
| curtime() |
+-----------+
| 20:37:05 |
+-----------+
1 row in set (0.00 sec)
其中,下面的兩個時間函式等同於 curtime():
current_time()/current_time
select current_time();
+----------------+
| current_time() |
+----------------+
| 20:37:55 |
+----------------+
1 row in set (0.00 sec)
select current_time;
+--------------+
| current_time |
+--------------+
| 20:38:06 |
+--------------+
1 row in set (0.00 sec)
獲得當前 UTC 日期時間函式:utc_date(), utc_time(), utc_timestamp()
select utc_timestamp(), utc_date(), utc_time(),now();
+---------------------+------------+------------+---------------------+
| utc_timestamp() | utc_date() | utc_time() | now() |
+---------------------+------------+------------+---------------------+
| 2018-12-25 12:40:15 | 2018-12-25 | 12:40:15 | 2018-12-25 20:40:15 |
+---------------------+------------+------------+---------------------+
1 row in set (0.00 sec)
因為我國位於東八時區,所以本地時間 = UTC 時間 + 8 小時。UTC 時間在業務涉及多個國家和地區的時候,非常有用。
選取日期時間的各個部分:日期、時間、年、季度、月、日、小時、分鐘、秒、微秒
set @dt = '2018-12-25 22:15:30.123456';
select date(@dt);
+------------+
| date(@dt) |
+------------+
| 2018-12-25 |
+------------+
1 row in set (0.00 sec)
select time(@dt);
+-----------------+
| time(@dt) |
+-----------------+
| 22:15:30.123456 |
+-----------------+
1 row in set (0.00 sec)
select year(@dt);
+-----------+
| year(@dt) |
+-----------+
| 2018 |
+-----------+
1 row in set (0.00 sec)
select year('70-12-25'); --
+------------------+
| year('70-12-25') |
+------------------+
| 1970 |
+------------------+
1 row in set (0.00 sec)
select year('69-12-25');
+------------------+
| year('69-12-25') |
+------------------+
| 2069 |
+------------------+
1 row in set (0.00 sec)
要注意的是: 如果年份只有兩位數,那麼自動補全的機制是以預設時間1970.01.01為界限的,>= 70 的補全 19,< 70 的補全 20
select quarter(@dt); -- 第幾季度
+--------------+
| quarter(@dt) |
+--------------+
| 4 |
+--------------+
1 row in set (0.00 sec)
select month(@dt);
+------------+
| month(@dt) |
+------------+
| 12 |
+------------+
1 row in set (0.00 sec)
select week(@dt); -- 一年的第幾周
+-----------+
| week(@dt) |
+-----------+
| 51 |
+-----------+
1 row in set (0.00 sec)
select week(@dt, 0); -- 一年當中的第幾周,當index為偶數時,預設是以為週日作為一週的第一天
+--------------+
| week(@dt, 0) |
+--------------+
| 51 |
+--------------+
1 row in set (0.00 sec)
select week(@dt, 1); -- 當index為奇數時,表示一週的第一天是週一
+--------------+
| week(@dt, 1) |
+--------------+
| 52 |
+--------------+
1 row in set (0.00 sec)
select day(@dt); -- 一個月中的第多少天
+----------+
| day(@dt) |
+----------+
| 25 |
+----------+
1 row in set (0.00 sec)
select hour(@dt); -- 一天中的小時
+-----------+
| hour(@dt) |
+-----------+
| 22 |
+-----------+
1 row in set (0.00 sec)
select minute(@dt); -- 小時中的分鐘
+-------------+
| minute(@dt) |
+-------------+
| 15 |
+-------------+
1 row in set (0.01 sec)
select second(@dt); -- 時間中的秒
+-------------+
| second(@dt) |
+-------------+
| 30 |
+-------------+
1 row in set (0.00 sec)
Extract() 函式,可以上面實現類似的功能:
set @dt = '2018-12-25 22:15:30.123456';
select extract(year from @dt);
+------------------------+
| extract(year from @dt) |
+------------------------+
| 2018 |
+------------------------+
1 row in set (0.00 sec)
select extract(quarter from @dt);
+---------------------------+
| extract(quarter from @dt) |
+---------------------------+
| 4 |
+---------------------------+
1 row in set (0.00 sec)
select extract(month from @dt);
+-------------------------+
| extract(month from @dt) |
+-------------------------+
| 12 |
+-------------------------+
1 row in set (0.00 sec)
select extract(week from @dt); -- 一年中的第多少周
+------------------------+
| extract(week from @dt) |
+------------------------+
| 51 |
+------------------------+
1 row in set (0.00 sec)
select extract(day from @dt); -- 當月的第多少天
+-----------------------+
| extract(day from @dt) |
+-----------------------+
| 25 |
+-----------------------+
1 row in set (0.00 sec)
select extract(hour from @dt); -- 時間中的小時
+------------------------+
| extract(hour from @dt) |
+------------------------+
| 22 |
+------------------------+
1 row in set (0.00 sec)
select extract(minute from @dt); -- 時間中的分鐘
+--------------------------+
| extract(minute from @dt) |
+--------------------------+
| 15 |
+--------------------------+
1 row in set (0.00 sec)
select extract(second from @dt); -- 時間中的秒
+--------------------------+
| extract(second from @dt) |
+--------------------------+
| 30 |
+--------------------------+
1 row in set (0.01 sec)
select extract(microsecond from @dt); -- 時間中的微秒
+-------------------------------+
| extract(microsecond from @dt) |
+-------------------------------+
| 123456 |
+-------------------------------+
1 row in set (0.00 sec)
select extract(year_month from @dt); -- 年月
+------------------------------+
| extract(year_month from @dt) |
+------------------------------+
| 201812 |
+------------------------------+
1 row in set (0.00 sec)
select extract(day_hour from @dt); -- 天和小時
+----------------------------+
| extract(day_hour from @dt) |
+----------------------------+
| 2522 |
+----------------------------+
1 row in set (0.00 sec)
select extract(day_minute from @dt); -- 天到分鐘
+------------------------------+
| extract(day_minute from @dt) |
+------------------------------+
| 252215 |
+------------------------------+
1 row in set (0.00 sec)
select extract(day_second from @dt); -- 天到秒
+------------------------------+
| extract(day_second from @dt) |
+------------------------------+
| 25221530 |
+------------------------------+
1 row in