MySQL built-in date processing function

Source: Internet
Author: User

The following table lists the most important built-in date functions in MySQL:

function Description
Now () Returns the current date and time
Curdate () Returns the current date
Curtime () Returns the current time
DATE () Extract date part of date or date/time expression
EXTRACT () Returns a separate part of the date/time Press
Date_add () Add a specified time interval to a date
Date_sub () Subtract a specified time interval from a date
DATEDIFF () Returns the number of days between two dates
Date_format () Display Date/time in a different format

Time difference function (Timestampdiff, DATEDIFF)

Need to use MySQL to calculate the time difference, using Timestampdiff, DATEDIFF, record the results of the experiment

--0Select DateDiff(now (), now ());--2Select DateDiff('2015-04-22 23:59:00','2015-04-20 00:00:00');--2Select DateDiff('2015-04-22 00:00:00','2015-04-20 23:59:00');--1SelectTimestampdiff ( Day,'2015-04-20 23:59:00','2015-04-22 00:00:00');--2SelectTimestampdiff ( Day,'2015-04-20 00:00:00','2015-04-22 00:00:00');--2SelectTimestampdiff ( Day,'2015-04-20 00:00:00','2015-04-22 12:00:00');--2SelectTimestampdiff ( Day,'2015-04-20 00:00:00','2015-04-22 23:59:00');-- inSelectTimestampdiff (HOUR,'2015-04-20 00:00:00','2015-04-22 23:00:00');--4260SelectTimestampdiff (MINUTE,'2015-04-20 00:00:00','2015-04-22 23:00:00');
Date conversion calculation functions (Date_add, Day, Date_format, str_to_date)
--use date and string conversions to calculate the first day of the month, the first day of the next monthSelectCurdate () as 'Current Date', Date_format (Curdate (),'%y-%m') as 'Current Month', Str_to_date (Concat (Date_format (Curdate (),'%y-%m'),'-01'),'%y-%m-%d') as 'the first day of the current month', Date_add (Str_to_date (Concat (Date_format) (Curdate (),'%y-%m'),'-01'),'%y-%m-%d'), Interval1 Month) as 'the first day of next month';--the last day of the current monthSelectLast_day (Curdate ());--the first day of next monthSelectDate_add (Last_day (Curdate ()), interval1  Day);--the day of the monthSelect  Day(Curdate ());--first day of the monthSelectDate_add (Curdate (), Interval1-( Day(Curdate ())) Day);

MySQL built-in date processing function

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.