MySQL various time function

1. Take the time before one month:

1) mysql> select date_sub(curdate(), interval 1 month); #The date one month before the current date

+---------------------------------------+
| date_sub(curdate(), interval 1 month) |
+---------------------------------------+
| 2017-03-28                            |
+---------------------------------------+

2) mysql> select date_sub(now(), interval 1 month); #The time one month before the current date

+-----------------------------------+
| date_sub(now(), interval 1 month) |
+-----------------------------------+
| 2017-03-28 16:00:04               |
+-----------------------------------+

 

2. Specify the acquisition time format:

1) mysql> select date_format(date_sub(now(), interval 1 month), '%y-%m-%d'); #How many years, months and days are before the current date one month

+------------------------------------------------------------+
| date_format(date_sub(now(), interval 1 month), '%y-%m-%d') |
+------------------------------------------------------------+
| 17-03-28                                                   |
+------------------------------------------------------------+

2) mysql> select date_format(date_sub(now(), interval 1 month), '%Y-%M-%D'); #How many years, months and days are before the current date one month

+------------------------------------------------------------+
| date_format(date_sub(now(), interval 1 month), '%Y-%M-%D') |
+------------------------------------------------------------+
| 2017-March-28th                                            |
+------------------------------------------------------------+

3) mysql> select date_format(date_sub(now(), interval 1 month), '%y-%m'); # How many years and months are before the current date one month

+---------------------------------------------------------+
| date_format(date_sub(now(), interval 1 month), '%y-%m') |
+---------------------------------------------------------+
| 17-03                                                   |
+---------------------------------------------------------+

 

3. Date addition and subtraction

1) mysql> select interval 1 month + '2017-04-28'; # What is the date 1 month after a certain date

+---------------------------------+
| interval 1 month + '2017-04-28' |
+---------------------------------+
| 2017-05-28                      |
+---------------------------------+

2) mysql> select '2017-04-28' + interval 10 day; # What is the date 10 days after a certain date

+--------------------------------+
| '2017-04-28' + interval 10 day |
+--------------------------------+
| 2017-05-08                     |
+--------------------------------+

3) mysql> select '2017-04-28' - interval 10 day; # What is the date 10 days before a certain date

+--------------------------------+
| '2017-04-28' - interval 10 day |
+--------------------------------+
| 2017-04-18                     |
+--------------------------------+

 

4. Time format modification

1) mysql> select date_format(now(), '%W %M %Y'); #Modify the time format to week month year format

+--------------------------------+
| date_format(now(), '%W %M %Y') |
+--------------------------------+
| Friday April 2017              |
+--------------------------------+

2) mysql> select date_format(now(), '%H:%i:%s'); #24-hour time display

+--------------------------------+
| date_format(now(), '%H:%i:%s') |
+--------------------------------+
| 16:22:32                       |
+--------------------------------+

3) mysql> select date_format(now(), '%h:%i:%s'); #12-hour time display

+--------------------------------+
| date_format(now(), '%h:%i:%s') |
+--------------------------------+
| 04:23:05                       |
+--------------------------------+

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324399254&siteId=291194637