2 Replies - 1598 Views - Last Post: 17 July 2018 - 04:49 AM

#1 CodeChick   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 19-June 16

DateTime to unix time in one line

Posted 17 July 2018 - 02:04 AM

Hey, guys

Is it possible to convert DateTime to Unix milliseconds in one line, without making methods for it, etc?

The thing is that I'm freelancing for a company, and the guys specializing on the particular module I'm currently using are on vacation. What is used in this module is a mix of .NET/C# code together with some other code languages, like a custom language. The former is used in brackets, and is then inserted into the rest of the code. So it's possible to get a nice date format like this for example:

{{%CHOSEN_START_TIME().AddMonths(-6).ToString("MM/yy")}}


However, I would like to convert such a date to unix time with milliseconds the format using 13 digits.

I've tried the following, but they don't work:

{{%CHOSEN_START_TIME().AddMonths(-6).ToUnixMilliseconds()}}


{{(TimeZoneInfo.ConvertTimeToUtc(%CHOSEN_START_TIME().AddMonths(-6)) - new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalMilliSeconds}} 


I'm really not that familiar with .NET and C#, so any help would be greatly appreciated. Thanks in advance. :)

Is This A Good Question/Topic? 0
  • +

Replies To: DateTime to unix time in one line

#2 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6748
  • View blog
  • Posts: 27,785
  • Joined: 12-December 12

Re: DateTime to unix time in one line

Posted 17 July 2018 - 03:39 AM

Isn't it ToUnixTimeMilliseconds?

Best to check and test C# expressions outside of that custom language, via VS or LINQPad.

Otherwise, please describe what happens with those expressions; are there errors.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7403
  • View blog
  • Posts: 15,340
  • Joined: 16-October 07

Re: DateTime to unix time in one line

Posted 17 July 2018 - 04:49 AM

Hmm... first google hit for DateTime to Unix milliseconds was Current Millis ‐ Milliseconds since Unix Epoch, which seems eminently apropos.

You could say, "yeah, sure, but DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() doesn't help for CHOSEN_START_TIME X." And you would be correct. However, it should be enough to let you find the answer.

Since we're dealing with .NET, I fired up PowerShell.
PS> [System.DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
1531827042531
PS> # cool, wonder if I can get System.DateTimeOffset for a different value
PS> [System.DateTimeOffset]::new([datetime]::Now).ToUnixTimeMilliseconds()
1531827766064
PS> # oops, do I know those are the same?  Can't type that quick, so...
PS> [System.DateTimeOffset]::new([datetime]::Now).ToUnixTimeMilliseconds() - [System.DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
0
PS> # yep.  So, now for an arbitrary value
PS> $chosenStartTime = [datetime]::Now
PS> $x = $chosenStartTime.AddMonths(-6)
PS> $x

Wednesday, January 17, 2018 7:44:49 AM

PS> [System.DateTimeOffset]::new($x).ToUnixTimeMilliseconds()
1516193089488
PS> # ta dada



That should be enough to get you there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1