DateDiff function to add or subtract a date plan

Source: Internet
Author: User
Tags current time date1 getdate

Syntax
 
DateDiff (interval, date1, date2 [, firstdayofweek [, firstweekofyear])

The DateDiff function syntax contains the following naming parameters:

This function calculates the number of date parts between two specified dates. The result is a signed integer equal to (date2-date1) in the date section.

When the result is not an even multiple of the date part, DATEDIFF is truncated instead of rounded.

When day is used as the date part, DATEDIFF returns the number of midnight between two specified times (including the second date but not the first date.

When month is used as the date part, DATEDIFF returns the number of the first day of the month between two dates (including the second date but not the first date.

When week is used as the date part, DATEDIFF returns the number of Sundays between two dates (including the second but not the first.


Use datediff to obtain the timestamp

The code is as follows: Copy code

1> SELECT datediff (ss, '2017-01-01 ', GETDATE () as time
2> go
Time
1361988498
(1 row affected)
1>

Datediff

Ss indicates in seconds

The timestamp is calculated from this date.

Getdate () current time

The number of seconds between the current read time and is the current timestamp.

Of course, if you can use getdate () to read the current date, you can also use other languages to convert it into a timestamp, such as js and php.

Of course, the datediff function is very powerful, for example, to calculate the number of months between two dates

The code is as follows: Copy code

1> SELECT datediff (mm, '2017-01-01 ', GETDATE () as time
2> go time13 (1 row affected)

The following statement returns 1:

The code is as follows: Copy code

Select datediff (hour, ''4: 00am'', ''5: 50am '')

The following statement returns 102:

The code is as follows: Copy code

Select datediff (month, ''1987/05/02 '', ''1995/11/15 '')

The following statement returns 0:

The code is as follows: Copy code

Select datediff (day, ''00: 00 '', ''23: 59 '')

The following statement Returns 4:

The code is as follows: Copy code

Select datediff (day, '''2014/1/19 ''', ''2014/2/23 '')

The following statement returns 0:

The code is as follows: Copy code

Select datediff (month, ''1999/07/19 '', ''1999/07/23 '')

The following statement returns 1:

The code is as follows: Copy code

Select datediff (month, ''1999/07/19 '', ''1999/08/23 '')

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.