Forum Discussion

gutovanzin's avatar
gutovanzin
Frequent Visitor
5 years ago
Solved

Get current time in UNIX timestamp

Hello!

 

I created a blank Query that returns the current date and time in ISO format.

= DateTimeZone.SwitchZone(DateTimeZone.LocalNow(),-3,0)
Returns: 2021-01-13T16:03:28.8506372-03:00

 

Is it possible to get the same result however on UNIX Timestamp? Like:

1610519337000

 

Best, 
Luis

 

  • Try this gutovanzin 

     

     

     = Duration.TotalSeconds(DateTime.LocalNow() - #datetime(1970,1,1,0,0,0))

     

     

    It simply gives you the total seconds from Jan 1, 1970 at midnight through now. Replace my DateTime.LocalNow() with your DateTimeZone.SwitchZone() function and it will work just fine.

     

    You can round that with Number.Round if you don't need all of the decimals.

     

6 Replies

  • edhans's avatar
    edhans
    Community Champion

    Try this gutovanzin 

     

     

     = Duration.TotalSeconds(DateTime.LocalNow() - #datetime(1970,1,1,0,0,0))

     

     

    It simply gives you the total seconds from Jan 1, 1970 at midnight through now. Replace my DateTime.LocalNow() with your DateTimeZone.SwitchZone() function and it will work just fine.

     

    You can round that with Number.Round if you don't need all of the decimals.

     

    • gutovanzin's avatar
      gutovanzin
      Frequent Visitor

      Hello!
      Thanks for the help, i'm trying to get my timezone, but got this error:

       

      = Duration.TotalSeconds(DateTimeZone.SwitchZone(DateTime.LocalNow(),-3) - #datetime(1970,1,1,0,0,0))

       

      Expression.Error: We cannot convert the value #datetime(2021, 1, 14, 8, 40, 13.0448086) to type DateTimeZone.
      Details:
      Value=14/01/2021 08:40:13
      Type=[Type]

       

      Any suggestion?
      Thanks!

      • edhans's avatar
        edhans
        Community Champion

        You cannot use DateTime.LocalNow in a timezone function. It doesn't have any timezone data. You need to use DateTimeZone.FixedLocalNow or DateTimeZone.LocalNow.

         

        However, I am not certian you should use any of those anyway. Unix Epoch time is always UTC +0, and when you load this report in the service, DateTime.LocalNow() as I used above also calculate as UTC +0, so it will be apples to apples. Once you get it into Unix Epoch time, then add (3600 * n) seconds to your final answer, where N is your timezone offset.

  • edhans's avatar
    edhans
    Community Champion

    gutovanzin - I have written a more complete article on this at my blog site. It won't go live until 9am Pacific on Friday, Jan 15 though. But you can see it then. In the mean time, if you have any questions let me know. Otherwise if you could give thumbs up to any posts that have helped and mark one or more as the solution so this thread can be known to be solved it would be appreciated.

  • Anonymous's avatar
    Anonymous
    Not applicable

    If you are looking for a formula to the the current UTC Epoch Time, try this:

    = Number.Round(Duration.TotalSeconds(DateTimeZone.UtcNow() - DateTimeZone.FromText("1970-01-01T00:00:00Z")))

     

    If you need the value in millisecond UTC Epoch Time, use this:

     

    = Number.Round(Duration.TotalSeconds(DateTimeZone.UtcNow() - DateTimeZone.FromText("1970-01-01T00:00:00Z")) * 1000)