MySQL gets the current date time (and the time conversion)

Source: Internet
Author: User
Tags date1 local time month name mysql manual

1.1 Get the current date + time (date + times) function: Now ()
In addition to the now () function to get the current date time, MySQL has the following function
Current_timestamp ()   current_timestamp
LocalTime ()   localtime
Localtimestamp ()   localtimestamp   
These date-time functions are equivalent to now (). Since the now () function is short and easy to remember, it is recommended that you always use now () instead of the functions listed above.
1.2 Get the current date + time (date + times) function: Sysdate ()
The Sysdate () date-time function is similar to now (), except that it is obtained at the execution start value, and sysdate () dynamically gets the value when the function executes.
2. Get the current date (date) function: Curdate ()
Where the following two date functions are equivalent to Curdate (): Current_date (), current_date
3. Get the current time function: Curtime ()
Where the following two time functions are equivalent to Curtime (): Current_time (), Current_time
4. Get the current UTC datetime function: Utc_date (), Utc_time (), Utc_timestamp ()
Because our country is located in the East Eight time zone, local time = UTC time + 8 hour. UTC time is useful when the business involves multiple countries and regions.
Second, MySQL date time Extract (select) function.
1. Select each part of the DateTime: Date, time, year, quarter, month, day, hour, minute, second, microsecond
Set @dt = ' 2008-09-10 07:15:30.123456 ';
Select Date (@dt); -2008-09-10
Select time (@dt); --07:15:30.123456
Select year (@dt); --2008
Select quarter (@dt); --3
Select Month (@dt); --9
Select Week (@dt); --36
Select Day (@dt); --10
Select Hour (@dt); --7
Select minute (@dt); --15
Select second (@dt); --30
Select Microsecond (@dt); --123456
2. MySQL Extract () function, you can implement similar functions above:
Set @dt = ' 2008-09-10 07:15:30.123456 ';
Select Extract (year from @dt); --2008
Select Extract (quarter from @dt); --3
Select Extract (month from @dt); --9
Select Extract (week from @dt); --36
Select Extract (Day from @dt); --10
Select Extract (hour from @dt); --7
Select extract (minute from @dt); --15
Select Extract (second from @dt); --30
Select Extract (microsecond from @dt); --123456
Select Extract (Year_month from @dt); --200809
Select Extract (Day_hour from @dt); --1007
Select Extract (Day_minute from @dt); --100715
Select Extract (Day_second from @dt); --10071530
Select Extract (Day_microsecond from @dt); --10071530123456
Select Extract (Hour_minute from @dt); --715
Select Extract (Hour_second from @dt); --71530
Select Extract (Hour_microsecond from @dt); --71530123456
Select Extract (Minute_second from @dt); --1530
Select Extract (Minute_microsecond from @dt); --1530123456
Select Extract (Second_microsecond from @dt); --30123456
The MySQL Extract () function has no function other than date (), Time (), and all other functions one should. And also has the choice ' day_microsecond ' and so on function. Note that this is not just the day and the microsecond, but the part of the date that has been selected to the Microsecond section.
The only downside to the MySQL Extract () function is that you need to hit the keyboard a few more times.
3. MySQL dayof ... Function: DayOfWeek (), DayOfMonth (), DayOfYear ()
Returns the position of the date parameter, in one week, month, year, respectively.
Set @dt = ' 2008-08-08 ';
Select DayOfWeek (@dt); --6
Select DayOfMonth (@dt); --8
Select DayOfYear (@dt); --221
The date ' 2008-08-08′ is the 6th day of the week (1 = Sunday, 2 = Monday, ..., 7 = Saturday), the 8th day of January, and the No. 221 day of the year.
4. MySQL Week ... Functions: Week (), WeekOfYear (), DayOfWeek (), Weekday (), Yearweek ()
Set @dt = ' 2008-08-08 ';
Select Week (@dt); --31
Select Week (@dt, 3); --32
Select WeekOfYear (@dt); --32
Select DayOfWeek (@dt); --6
Select Weekday (@dt); --4
Select Yearweek (@dt); --200831
MySQL week () function, can have two parameters, specific to see the manual. WeekOfYear () and week () are calculated as "one day" in the first week of the year. WeekOfYear (@dt) is equivalent to week (@dt, 3).
The MySQL Weekday () function, like DayOfWeek (), returns the position of "one day" in a week. The difference is in reference to the standard, weekday: (0 = Monday, 1 = Tuesday, ..., 6 = Sunday); DayOfWeek: (1 = Sunday, 2 = Monday, ..., 7 = Saturday)
MySQL Yearweek () function, returns the year (+) + Week position (31).
5. MySQL returns the week and month name functions: Dayname (), MonthName ()
Set @dt = ' 2008-08-08 ';
Select Dayname (@dt); --Friday
Select MonthName (@dt); --August
6. MySQL Last_day () function: Returns the last day of the month.
Select Last_day (' 2008-02-01 '); -2008-02-29
Select Last_day (' 2008-08-08 '); -2008-08-31
Third, MySQL date time calculation function
1. MySQL adds a time interval for the date: Date_add ()
Set @dt = Now ();
Select Date_add (@dt, Interval 1 day); --Add 1 day
Select Date_add (@dt, interval 1 hour); --Add 1 hour
Select Date_add (@dt, interval 1 minute); -- ...
Select Date_add (@dt, Interval 1 second);
Select Date_add (@dt, interval 1 microsecond);
Select Date_add (@dt, Interval 1 week);
Select Date_add (@dt, interval 1 month);
Select Date_add (@dt, interval 1 quarter);
Select Date_add (@dt, Interval 1); Select Date_add (@dt, Interval-1 Day); --SUB 1 day
MySQL adddate (), Addtime () function, can be replaced with Date_add (). The following is an example of the date_add () implementation of the Addtime () feature:
mysql> Set @dt = ' 2008-08-09 12:12:33 ';
Mysql> Select Date_add (@dt, Interval ' 01:15:30 ' Hour_second);
+------------------------------------------------+
| Date_add (@dt, Interval ' 01:15:30 ' Hour_second) |
+------------------------------------------------+
| 2008-08-09 13:28:03 |
+------------------------------------------------+
Mysql> Select Date_add (@dt, Interval ' 1 01:15:30 ' Day_second);
+-------------------------------------------------+
| Date_add (@dt, Interval ' 1 01:15:30 ' Day_second) |
+-------------------------------------------------+
| 2008-08-10 13:28:03 |
+-------------------------------------------------+
The Date_add () function adds "1 hours, 15 minutes, 30 seconds" and "1 days, 1 hours, 15 minutes, 30 seconds" for @dt respectively.
Recommendation: Always use the Date_add () date-time function instead of adddate (), Addtime (). <BR><br>
2. MySQL for date minus one time interval: Date_sub ()
MySQL date_sub () datetime functions and Date_add () are used in a consistent, no-repeat. In addition, MySQL also has two functions subdate (), Subtime (), recommended, with Date_sub () to replace.
3. MySQL Alternative Date function: Period_add (p,n), Period_diff (P1,P2)
The format of the function parameter "P" is "yyyymm" or "yymm", and the second parameter "n" means increment or subtract N month (month).
MySQL Period_add (p,n): Date plus/minus N months.
4. MySQL date, time subtraction function: DateDiff (DATE1,DATE2), Timediff (time1,time2)
MySQL DateDiff (DATE1,DATE2): Two date subtraction Date1-date2, returns the number of days.
Select DateDiff (' 2008-08-08 ', ' 2008-08-01 '); --7
Select DateDiff (' 2008-08-01 ', ' 2008-08-08 '); ---7
MySQL Timediff (time1,time2): Two date subtraction time1-time2, returns the time difference value.
Select Timediff (' 2008-08-08 08:08:08 ', ' 2008-08-08 00:00:00 '); -08:08:08
Select Timediff (' 08:08:08 ', ' 00:00:00 '); -08:08:08
Note: the Timediff (time1,time2) function must have the same two parameter types.
Four, MySQL date conversion function, time conversion function
1. MySQL (time, seconds) conversion function: Time_to_sec (times), sec_to_time (seconds)
Select Time_to_sec (' 01:00:05 '); --3605
Select Sec_to_time (3605); --' 01:00:05 '
2. MySQL (date, day) conversion functions: to_days (date), From_days (days)
Select To_days (' 0000-00-00 '); --0
Select To_days (' 2008-08-08 '); --733627
Select From_days (0); --' 0000-00-00 '
Select From_days (733627); --' 2008-08-08 '
3. MySQL str to date (string conversion to date) function: Str_to_date (str, format)
Select Str_to_date (' 08/09/2008 ', '%m/%d/%y '); -2008-08-09
Select Str_to_date (' 08/09/08 ', '%m/%d/%y '); -2008-08-09
Select Str_to_date (' 08.09.2008 ', '%m.%d.%y '); -2008-08-09
Select Str_to_date (' 08:09:30 ', '%h:%i:%s '); -08:09:30
Select Str_to_date (' 08.09.2008 08:09:30 ', '%m.%d.%y%h:%i:%s '); --2008-08-09 08:09:30
As you can see, the Str_to_date (Str,format) conversion function allows you to convert some disorganized strings into a date format. In addition, it can also be converted to time. "Format" can be see the MySQL manual.
4. MySQL Date/time to STR (date/time converted to string) function: Date_format (Date,format), Time_format (Time,format)
MySQL date, time conversion function: Date_format (Date,format), Time_format (Time,format) is able to convert a date/time into a variety of string formats. It is an inverse conversion of the str_to_date (Str,format) function.
5. MySQL gets the national region time Format function: Get_format ()
MySQL Get_format () syntax:
Get_format (Date|time|datetime, ' EUR ' | ' USA ' | ' JIS ' | ' ISO ' | ' Internal
All examples of MySQL Get_format () Usage:
Select Get_format (date, ' USA '); --'%m.%d.%y '
Select Get_format (date, ' JIS '); --'%y-%m-%d '
Select Get_format (date, ' ISO '); --'%y-%m-%d '
Select Get_format (date, ' EUR '); --'%d.%m.%y '
Select Get_format (date, ' internal '); --'%y%m%d '
Select Get_format (datetime, ' USA '); --'%y-%m-%d%h.%i.%s '
Select Get_format (datetime, ' JIS '); --'%y-%m-%d%h:%i:%s '
Select Get_format (datetime, ' ISO '); --'%y-%m-%d%h:%i:%s '
Select Get_format (DateTime, ' EUR '); --'%y-%m-%d%h.%i.%s '
Select Get_format (datetime, ' internal '); --'%y%m%d%h%i%s '
Select Get_format (Time, ' USA '); --'%h:%i:%s%p '
Select Get_format (Time, ' JIS '); --'%h:%i:%s '
Select Get_format (Time, ' ISO '); --'%h:%i:%s '
Select Get_format (Time, ' EUR '); --'%h.%i.%s '
Select Get_format (Time, ' internal '); --'%h%i%s '
The MySQL Get_format () function uses less opportunity in practice.
6. MySQL Patchwork Date, time function: Makdedate (year,dayofyear), Maketime (Hour,minute,second)
Select Makedate (2001,31); --' 2001-01-31 '
Select Makedate (2001,32); --' 2001-02-01 ' select Maketime (12,15,30); --' 12:15:30 '
V. MySQL timestamp (Timestamp) function
1. MySQL Gets the current timestamp function: Current_timestamp, Current_timestamp ()
2. MySQL (Unix timestamp, date) conversion function:
Unix_timestamp (),
Unix_timestamp (date),
From_unixtime (Unix_timestamp),
From_unixtime (Unix_timestamp,format)
3. MySQL timestamp (timestamp) conversion, increment, decrement function:
Timestamp (date)--date to timestamp
Timestamp (dt,time)--DT + Time
Timestampadd (unit,interval,datetime_expr)--
Timestampdiff (UNIT,DATETIME_EXPR1,DATETIME_EXPR2)--
The MySQL Timestampdiff () function is much stronger than the DateDiff () function, and DateDiff () can only calculate the number of days between two dates (date).
Six, MySQL time zone (timezone) conversion function Convert_tz (DT,FROM_TZ,TO_TZ) Select Convert_tz (' 2008-08-08 12:00:00 ', ' +08:00 ', ' +00:00 '); --2008-08-08 04:00:00
Time zone conversions can also be achieved by Date_add, Date_sub, Timestampadd.
Select Date_add (' 2008-08-08 12:00:00 ', Interval-8 hour); --2008-08-08 04:00:00
Select Date_sub (' 2008-08-08 12:00:00 ', Interval 8 hour); --2008-08-08 04:00:00
Select Timestampadd (Hour,-8, ' 2008-08-08 12:00:00 '); --2008-08-08 04:00:00

MySQL gets the current date time (and the time conversion)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.