» » » 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

One Response

  1. Viet Anh Auto
    | Reply

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

Leave a Reply