We usually dealing with the data, the date of the general will encounter this point. Date format is the mysql date ().
MySQL DATE data type Description:
1, MySQL DATE
is used to manage five times the value of the date data type one. MySQL uses the yyyy-mm-dd
format to store date values. This format is fixed and can not change it .
For example, some people may prefer to use mm-dd-yyyy
format, but unfortunately, can not be used directly. Instead of a way: follow the standard date format and use the DATE_FORMAT function in the desired format to format dates.
2, MySQL using 3
bytes to store DATE
values. DATE
Range of values is 1000-01-01
to 9999-12-31
. To store date values beyond this range, it is necessary to use a non-time data type,
Such as integers, for example three, respectively, to store data year, month, and day. Also need to create a storage function to simulate the built-in date functions provided by MySQL, this is not recommended.
3, when the strict mode is disabled, MySQL any invalid date (for example 2015-02-30
) converted to zero date value 0000-00-00
. (We all know that the year is not 30 February)
SELECT NOW() AS cur_time;
DATE()
the function.
SELECT DATE(NOW()) AS cur_date;
SELECT CURDATE();
%m/%d/%Y
, the date format is:
mm/dd/yyyy
:
SELECT DATE_FORMAT(NOW(), '%d/%m/%Y') AS today;
V. To calculate the number of days between two dates values, may be used DATEDIFF functions:
SELECT DATEDIFF('2015-11-04','2014-11-04') days;
Sixth, to add days, weeks, months, years, until a date value, you can use DATE_ADD function:
SELECT '2018-01-01' start, DATE_ADD('2018-01-01', INTERVAL 1 DAY) 'one day later', DATE_ADD('2018-01-01', INTERVAL 1 WEEK) 'one week later', DATE_ADD('2018-01-01', INTERVAL 1 MONTH) 'one month later', DATE_ADD('2018-01-01', INTERVAL 1 YEAR) 'one year later';
Seven, can be used DATE_SUB subtracts from the date of the interval values:
SELECT '2018-01-01' start, DATE_SUB('2018-01-01', INTERVAL 1 DAY) 'one day before', DATE_SUB('2018-01-01', INTERVAL 1 WEEK) 'one week before', DATE_SUB('2018-01-01', INTERVAL 1 MONTH) 'one month before', DATE_SUB('2018-01-01', INTERVAL 1 YEAR) 'one year before';
Eight, if you want to get a date value date, month, quarter and year, you can use the corresponding functions: DAY, MONTH, QUARTER and YEAR, as follows:
SELECT DAY('2020-01-04') day, MONTH('2020-01-04') month, QUARTER('2020-01-04') quarter, YEAR('2020-01-04') year;
Nine, if you want to get information week. Can WEEK return a few weeks function, WEEKDAY
the function returns the weekday index, WEEKOFYEAR
the function returns the Week.
SELECT WEEKDAY('2020-01-04') weekday, WEEK('2020-01-04') week, WEEKOFYEAR('2020-01-04') weekofyear;
Other relevant information function can check their dates.