Forum Discussion

nassim's avatar
nassim
Occasional Contributor
14 years ago

How to change date time to unix time stamp

Hi,

I have to find out what is the unix timestamp at a specific point of my test. I know that I can write

now = aqDateTime.Now()

but how to change it to unix timestamp? Does anyone have an idea?

Thank you in advance,

Nassim
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Nassim,



    Try this:





    var unixTime = Math.round((new Date()).getTime() / 1000);





    Set dtUTC = GetObject("winmgmts:\\.\root\cimv2:Win32_UTCTime=@")

    dt = aqDateTime.SetDateTimeElements(dtUTC.Year, dtUTC.Month, dtUTC.Day, dtUTC.Hour, dtUTC.Minute, dtUTC.Second)

    unixTime = DateDiff("s", "01/01/1970 00:00:00", dt)

  • nassim's avatar
    nassim
    Occasional Contributor
    Hi Helen,

    Thank you very much for your help. Once, the VM windows time showsed 10:48 and I ran your solution like this:

    Set dtUTC = GetObject("winmgmts:\\.\root\cimv2:Win32_UTCTime=@")


    dt = aqDateTime.SetDateTimeElements(dtUTC.Year, dtUTC.Month, dtUTC.Day, dtUTC.Hour, dtUTC.Minute, dtUTC.Second)

    curTimeStr = aqConvert.DateTimeToStr(dt)


    Log.Message("current time is " & curTimeStr)

    unixTime = DateDiff("s", "01/01/1970 00:00:00", dt)

    Log.Message("unix time is " & unixTime)



    in Log I see:

    current time is 04.10.2011 08:48:37

    unix time is 1317718117  



    As I checked the unix time 1317718117 , it relates to the date

    Tue Oct 04 10:48:37 2011



    I am confused. It would be very kind if you or another member can help.

    Thank you in advance

    Nassim
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Nassim,



    Your computer is set to the GMT+2 time zone, right?



    The VBScript code that I posted uses the UTC/GMT time, so the lines that you added:



    curTimeStr = aqConvert.DateTimeToStr(dt)

    Log.Message("current time is " & curTimeStr)

    report the UTC/GMT time rather than your local time, hence the two-hour time difference.



    To log your local time, use the following code instead:



    Check out these links for more information:

    http://www.epochconverter.com/epoch/timezones.php?epoch=1317718117

    http://www.timestampgenerator.com