Mysql function to get date and time
mysql datetime functions | describe |
---|---|
NOW([fps]) / CURRENT_TIMESTAMP([fps]) / LOCALTIMESTAMP(fps) / LOCALTIME([fps]) |
Return the current date and time ( datetime ), (parameter fps is optional, the default is 2, second precision, value [0,6] ) |
CURDATE() / CURRENT_DATE() |
Returns the current date ( date ), only contains the year, month, and day, in the default format:YYYY-MM-DD |
CURTIME([fps]) /CURRENT_TIME([fps]) |
Returns the current time ( time ), which only includes hours, minutes, and seconds (parameters fps are optional, default is 2, seconds precision, value [0,6] ), default format:YYYY-MM-DD HH:MM:SS |
UNIX_TIMESTAMP(date) |
Returns the UNIX timestamp of the current date date |
FROM_UNIXTIME |
Returns the date value of a UNIX timestamp |
MONTHNAME(date) |
Returns the English name of the month of the date date |
SELECT DAYNAME(NOW()) |
Returns the English name of the day of the week of date |
DATE_FORMAT(date, fmt) |
Returns the date value formatted by the string fmt |
DATE_ADD(date, INTERVAL expr unit) |
Returns a date/time date plus an interval expr. |
DATE_SUB(date, INTERVAL expr unit) |
Returns a date/time date minus an interval expr. |
DATEDIFF(expr1, expr1) |
Returns the number of days between the start time expr1 and the end time expr2 |
TIMEDIFF(expr1, expr2) |
Returns the number of days between two dates (time) date (expr1 - expr2) |
Function name | describe |
---|---|
YEAR(expr) | Returns the year part (format yyyy ) of the specified date and time, case: SELECT YEAR(NOW()) , returns the year of the current time |
MONTH(expr) | Returns the month part of the specified datetime |
DAY(expr) | Returns the **date** part of the specified datetime |
HOUR(expr) | Returns the hour part of the specified datetime |
MINUTE(expr) | Returns the minutes portion of the specified datetime |
SECOND(expr) | Returns the seconds portion of the specified datetime |
MICROSECOND(expr) | Returns the microseconds part of the specified datetime |
QUARTER(expr) | Returns the quarter part of the specified datetime |
WEEK(expr) | Returns the week portion of the specified datetime |
DATE(expr) | Returns the date part of the specified datetime |
TIME(expr) | Returns the time part of the specified datetime |
1. The main function to get the current datetime (datetime): NOW()
andSYSDATE()
The difference between functions NOW()
and SYSDATE()
:
NOW()
BothSYSDATE()
represent the current time, but there is a little difference in usage.
NOW()
What is taken is the time when the statement starts to execute , andSYSDATE()
what is taken is the dynamic real-time time .
This is becauseNOW()
is taken from a variable of mysqlTIMESTAMP
, and this variable is set when the statement starts to execute, so it will not change during the entire statement execution.
For example, the following SQL query:SELECT NOW(),SYSDATE(), SLEEP(2) AS '等待2s', NOW(),SYSDATE()
Query result:
It is found that the time of the two queriesNOW()
is the same, butSYSDATE()
the time of the two times is different, which is a dynamic real-time time.
We generally useNOW()
instead ofSYSDATE()
the function
There are also three functions to get the time, the effect is the NOW()
same as that of
LOCALTIME()
LOCALTIMESTAMP()
CURRENT_TIMESTAMP()
NOW([fps])
function
Return the current date and time of the server (datetime object)
parameter fsp
optional, used to specify the precision of fractional seconds, if not written, it will display 2 digits by default (value range [0,6]
)
The NOW() function returns two formats by default: YYYY-MM-DD HH:MM:SS
orYYYYMMDDHHMMSS
NOW()
: returnYYYY-MM-DD HH:MM:SS
format
NOW()
: returnYYYYMMDDHHMMSS
format
select NOW(), NOW()+0, NOW(0), NOW(6)
run:
2. Calculation function of date and time (addition and subtraction time interval)
DATE_ADD(date, INTERVAL expr type)
and DATE_SUB(date, INTERVAL expr type)
are the values after increasing/decreasing the time, respectively.
INTERVAL
It is a keyword, indicating the type of interval.
expr
It is an expression, that is, the value of the interval
unit
. It is the unit of the time interval. It mainly has the following value units (year, month, day, hour, minute, second, etc.)
The value of unit
unit time interval unit value | describe |
---|---|
YEAR | Year |
MONTH | moon |
DAY | Day |
HOUR | hour |
MINUTE | point |
SECOND | Second |
MICROSECOND | millisecond |
QUARTER | season (3 months) |
WEEK | week |
YEAR_MONTH | year and month |
DAY_HOUR | day and time |
DAY_MINUTE | weather forecast |
DAY_ SECOND | day and second |
HOUR_MINUTE | hours and minutes |
HOUR_SECOND | hours and seconds |
MINUTE_SECOND | minutes and seconds |
-- 给当前时间加三小时
SELECT NOW(),DATE_ADD(NOW(),interval 1+2 HOUR)
-- 2021-03-18 00:00:00 加18小时18分后的时间
SELECT DATE_ADD('2021-03-18 00:00:00',interval '18:18' HOUR_MINUTE)
Function EXTRACT(unit FROM datetime)
to extract a single part or a combination of parts from a datetime.
for example:
-- 从当前的日期时间中,抽取出 时分 组合部分
select EXTRACT(HOUR_MINUTE FROM NOW())