MySQL 获取系统时间/系统日期/日期时间的函数

首页 / 新闻资讯 / 正文

(一)now(),返回当前的系统日期和时间

now() 函数以 YYYY-MM-DD HH:MM:SS 返回系统当前的日期时间,可以直接存到 DATETIME 字段中。下面的 sysdate() 函数也是如此。

mysql> select now(); +---------------------+ | now()               | +---------------------+ | 2021-06-04 08:24:51 | +---------------------+ 1 row in set (0.00 sec) 

(二)sysdate(),返回当前的系统日期和时间

mysql> select sysdate(); +---------------------+ | sysdate()           | +---------------------+ | 2021-06-04 08:27:35 | +---------------------+ 1 row in set (0.00 sec) 

now()sysdate() 这两个函数都是获取当前日期+时间,不同之处在于:now() 在执行开始时值就得到了,sysdate() 在函数执行时动态得到值。

(三)current_timestamp(),返回当前的系统日期和时间

mysql> select current_timestamp(); +---------------------+ | current_timestamp() | +---------------------+ | 2021-06-04 14:12:37 | +---------------------+ 1 row in set (0.00 sec) 

(四)localtime(),返回当前的系统日期和时间

mysql> select localtime(); +---------------------+ | localtime()         | +---------------------+ | 2021-06-04 14:13:54 | +---------------------+ 1 row in set (0.00 sec) 

(一)curdate(),返回当前的系统日期

curdate()YYYY-MM-DD 的格式返回今天的日期,可以直接存到 DATE 字段中。下面函数两个也是如此。

mysql> select curdate(); +------------+ | curdate()  | +------------+ | 2021-06-04 | +------------+ 1 row in set (0.00 sec) 

(二)current_date(),返回当前的系统日期

mysql> select current_date(); +----------------+ | current_date() | +----------------+ | 2021-06-04     | +----------------+ 1 row in set (0.00 sec) 

(三)current_date,返回当前的系统日期

mysql> select current_date; +--------------+ | current_date | +--------------+ | 2021-06-04   | +--------------+ 1 row in set (0.00 sec) 

(一)curtime(),返回当前的系统时间

curtime()HH:MM:SS 的格式返回当前的时间,可以直接存到 TIME 字段中。以下两个函数也是如此。
举例如下:

mysql> select curtime(); +-----------+ | curtime() | +-----------+ | 10:55:13  | +-----------+ 1 row in set (0.03 sec) 

(二)current_time(),返回当前的系统时间

(三)current_time,返回当前的系统时间

协调世界时,又称世界统一时间,世界标准时间,国际协调时间,简称 UTC。

(一)utc_date(),获取 UTC 日期

mysql> select utc_date(); +------------+ | utc_date() | +------------+ | 2021-06-04 | +------------+ 1 row in set (0.15 sec) 

(二)utc_time(),获取 UTC 时间

mysql> select utc_time(); +------------+ | utc_time() | +------------+ | 06:47:26   | +------------+ 1 row in set (0.00 sec) 

(三)utc_timestamp(),获取 UTC 的日期和时间

mysql> select utc_timestamp(); +---------------------+ | utc_timestamp()     | +---------------------+ | 2021-06-04 07:04:35 | +---------------------+ 1 row in set (0.00 sec) 

编程狮(w3cschool.cn)

  含义
%S、%s 两位数字形式的秒( 00,01, ..., 59)
%I、%i 两位数字形式的分( 00,01, ..., 59)
小时  %H 24小时制,两位数形式小时(00,01, ...,23)
%h 12小时制,两位数形式小时(00,01, ...,12)
%k 24小时制,数形式小时(0,1, ...,23)
%l 12小时制,数形式小时(0,1, ...,12)
%T 24小时制,时间形式(HH:mm:ss)
%r  12小时制,时间形式(hh:mm:ss AM 或 PM)
%p  AM上午或PM下午 
  周   %W 一周中每一天的名称(Sunday,Monday, ...,Saturday)
 %a 一周中每一天名称的缩写(Sun,Mon, ...,Sat) 
%w  以数字形式标识周(0=Sunday,1=Monday, ...,6=Saturday) 
%U 数字表示周数,星期天为周中第一天
%u 数字表示周数,星期一为周中第一天
%d  两位数字表示月中天数(01,02, ...,31)
%e   数字表示月中天数(1,2, ...,31)
 %D 英文后缀表示月中天数(1st,2nd,3rd ...) 
 %j 以三位数字表示年中天数(001,002, ...,366) 
%M  英文月名(January,February, ...,December) 
%b  英文缩写月名(Jan,Feb, ...,Dec) 
%m  两位数字表示月份(01,02, ...,12)
%c  数字表示月份(1,2, ...,12) 
%Y  四位数字表示的年份(2015,2016...)
%y   两位数字表示的年份(15,16...)
文字输出  %文字  直接输出文字内容
Top