Home » Database » MySQL » Convert a DateTime string to a Unix timestamp in VbScript / Classic ASP

Convert a DateTime string to a Unix timestamp in VbScript / Classic ASP

To convert a datetime string value like “2016-01-01 12:30:00” to a Unix timestamp (the seconds from 01/01/1970 to the input datetime) You can use a conversion function as the following:

Function ConvertToUnixTimeStamp(input_datetime) 'As String 
  Dim d : d = CDate(input_datetime)
  ConvertToUnixTimeStamp = CStr(DateDiff("s", "01/01/1970 00:00:00", d))
End Function

 

Then, to get back the datetime from the Unix timestamp use the following code:

Function ConvertUnixTimeStampToDateTime(input_unix_timestamp) 'As String [regular datetime]
   ConvertUnixTimeStampToDateTime = CStr(DateAdd("s", input_unix_timestamp, "01/01/1970 00:00:00"))
End Function

 

3 Responses

  1. Viet Anh Auto
    | Reply

    I had the same problem. You saved my day. Thanks so much!

  2. Ashwani
    | Reply

    what if need to convert from filetime to date in classic asp

  3. China Gittler
    | Reply

    Hello there, You have done an excellent job. I’ll certainly digg it and personally recommend to my friends. I’m sure they will be benefited from this web site.|

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.