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 '') |