The DATEDIFF () function returns the number of days between two dates.
Syntax
The startdate AND enddate parameters of DATEDIFF (datepart, startdate, enddate) are valid date expressions.
Use the following SELECT statement:
The code is as follows: |
Copy code |
Select datediff (day, '2017-12-29 ', '2017-12-30') AS DiffDate result: DiffDate 1 |
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 Time 13 (1 row affected) |