MySQL calculates a time difference function of two dates

Source: Internet
Author: User
Tags time and seconds

MySQL calculates a difference function for two datetime:

The first: theTimestampdiff function, which needs to pass in three parameters, the first being the type of comparison, can compare Frac_second, SECOND, MINUTE, HOUR, Day, WEEK, MONTH, quarter, or Year of several types, the second and third parameters are two times to compare, the comparison is the latter time minus the previous time, the specific usage is as follows:

SELECT Timestampdiff (Day, ' 2012-10-01 ', ' 2013-01-13 ');
The return result is 104, here is the difference between the days of two times;

SELECT Timestampdiff (MONTH, ' 2012-10-01 ', ' 2013-01-13 ');

The comparison here is the month of two times, the return result is 3;

The second method: The DateDiff function, passing in the two date function, compares the day days, the first parameter minus the second parameter's days value, the following is used as follows:

SELECT DATEDIFF (' 2013-01-13 ', ' 2012-10-01 ');
The returned result is also 104.

Another date function,

The now () function returns the date of the current time and seconds, such as:2008-12-29 16:25:46

The Curdate () function returns the month and day information: for example:2008-12-29

The Curtime () function returns the time-of-day information, such as:16:25:46

Also, if we want to format a date with a date that contains a month or two, you can use the DATE function, such as

DATE (now ()) returns the 2008-12-29


MySQL calculates a time difference function of two dates

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.