Blog Stats
  • Posts - 7
  • Articles - 0
  • Comments - 12
  • Trackbacks - 14

 

Converting Windows time to Unix time, with VBscript

Some command line tools ask for the UNIX time. If you’re using Jscript, this is no a problem. But VBscript doesn’t have a function that does this.

 

So here is some nice one-liner that gives the UNIX time

 

WScript.echo DateDiff("s", "12/31/1969 00:00:00", _
 DateSerial(Year(Now), Month(Now), Day(Now)) _
 + TimeSerial(Hour(Now), Minute(Now), Second(Now)))


Feedback

# re: Converting Windows time to Unix time, with VBscript

Gravatar I was trying to do this in .NET, and although your function is close it wasn't close enough that I could match md5 hashes from a php unix epoch to the .net. The following function worked though:

' Convert a local windows time to UTC and turn it
' into a unix timestamp by diffing it with unix EPOCH
' EPOCH: Private EPOCH As New Date(1970, 1, 1, 0, 0, 0)
Private Function WindowsTimeToUnixTime(ByVal Convert As Date) As Long
Return DateDiff(DateInterval.Second, EPOCH, Convert.ToUniversalTime())
End Function 10/5/2005 12:05 PM | Mike

# re: Converting Windows time to Unix time, with VBscript

Gravatar Hi there,

I just tested your function and found that the date you have there is wrong. You should be using "1/1/1970 00:00:00" instead of 12/31/1969 (as per the definition of EPOCH time).

So the correct one should be:
WScript.echo DateDiff("s", "1/1/1970 00:00:00", _
DateSerial(Year(Now), Month(Now), Day(Now)) _
+ TimeSerial(Hour(Now), Minute(Now), Second(Now)))

Yet, thanks for the function because i saved me a lot of time! 8^)

Stephen
4/1/2009 9:31 AM | Stephen

# re: Converting Windows time to Unix time, with VBscript

Gravatar a little bit nicer, here's what I use:

WScript.Echo DateDiff("s", "12/31/1969 00:00:00", Now)

The Now function returns the current date and time. 7/1/2009 6:31 PM | austin

Post a comment





 

 

 

Copyright © Michel Klomp