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

Print | posted @ Sunday, August 28, 2005 2:44 PM

Comments on this entry:

Gravatar # re: Converting Windows time to Unix time, with VBscript
by Stephen at 4/1/2009 9:31 AM

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
Gravatar # re: Converting Windows time to Unix time, with VBscript
by austin at 7/1/2009 6:31 PM

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.
Gravatar # re: Converting Windows time to Unix time, with VBscript
by fi-side yeni kampanya at 3/20/2011 10:07 PM

thank you very good a post
Gravatar # re: Converting Windows time to Unix time, with VBscript
by Chris at 12/30/2011 4:03 AM

shouldn't the date be Jan 1, 1970?
Gravatar # re: Converting Windows time to Unix time, with VBscript
by DOQ at 4/24/2012 1:17 PM

One problem with these, they do not allow for your GMT offset. How do you do that without hardcoding your offset? If you produce this time and then convert that back (maybe use Perl to check it out?), you will find that you are off by your GMT (or UTC) offset.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: