-2

In C# I would like to get UTC (+00:00) time as milliseconds. So I can use this in Javascript with offset(like below). I have tried several things but I did not achieve this.

new Date(1528204115692 - (new Date().getTimezoneOffset() * 60000)).toString()

Below code gives me milliseconds according to my timezone.

((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds()

I want to keep UTC time millisecond in db so I can show datetime according to user browser regional zone.

For example : in +03:00 zone now time is 06.05.2018 16:12:20.568

I want keep UTC zone time in milliseconds. (epoch time 00:00)

Can you Help?

Thank you

baris usanmaz
  • 839
  • 1
  • 13
  • 31
  • Javascript uses milliseconds starting from 1970/1/1 https://www.w3schools.com/jsref/jsref_gettime.asp . It is a little more complex. – xanatos Jun 05 '18 at 13:17
  • Are you looking for DateTime.UtcNow ? – K. Stefaniak Jun 05 '18 at 13:17
  • Possible duplicate of [DateTime's representation in milliseconds?](https://stackoverflow.com/questions/5955883/datetimes-representation-in-milliseconds) – Calvin Nunes Jun 05 '18 at 13:17
  • I don't know why this got 2 downvotes, it's a reasonable question. Maybe include some code next time. – Jeremy Thompson Jun 05 '18 at 13:18
  • In JS I wanto use this method: new Date(1528204115692 - (new Date().getTimezoneOffset() * 60000)).toString() – baris usanmaz Jun 05 '18 at 13:18
  • 3
    @JeremyThompson It would be a reasonable question if he wrote some contest. Does he want to round `DateTime.UtcNow`, or does he want `DateTime.UtcNow - new DateTime(1970, 1, 1)` (he is speaking of Javascript), or perhaps he wants ??? – xanatos Jun 05 '18 at 13:19
  • 1
    also, he didn't shown any attempt to achieve that – Calvin Nunes Jun 05 '18 at 13:19
  • Unclear `((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds()` what problem it has. It is even shorter than my code :-) – xanatos Jun 05 '18 at 13:26
  • Am I wrong? I am using in C# `((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds()` and I want to prove that in JS `new Date(1528204115692 - (new Date().getTimezoneOffset() * 60000)).toString()` – baris usanmaz Jun 05 '18 at 13:29
  • 1
    You are wrong, yes. You should not be subtracting any offset. The value you have from C# is in UTC, and the `Date` constructor in JavaScript takes its values in UTC. Unix Timestamps are *always* in terms of UTC. – Matt Johnson-Pint Jun 05 '18 at 17:49

2 Answers2

2

Your C# code was correct.

From mozilla:

new Date(value);

value

Integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC, with leap seconds ignored (Unix Epoch; but consider that most Unix timestamp functions count in seconds).

So you only need:

var date = new Date(1528204115692);

Where 1528204115692 is the value you obtain from your C# code.

Javascript dates are internally in milliseconds (it is simply a number) and "start" at 01 jan 1970 00.00 (that is "time" 0).

So:

public static readonly DateTime Date01Jan1970 = new DateTime(1970, 1, 1);

public static long MillisecondsFrom01Jan1970(DateTime dt)
{
    return (dt.Ticks - Date01Jan1970.Ticks) / TimeSpan.TicksPerMillisecond;
}

Use it like:

long ms = MillisecondsFrom01Jan1970(DateTime.UtcNow);

This will return the number of ms that passed between the DateTime.UtcNow (the "now" in Utc time) and the 01 jan 1970.

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Let me ask, your JS code gives the same result either in Russia or UK? `var date = new Date(1528204115692);` Also these C# codes are giving the same result, is not it wrong? `((DateTimeOffset)DateTime.Now).ToUnixTimeMilliseconds();` `((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds();` – baris usanmaz Jun 05 '18 at 13:53
  • @barisusanmaz `new Date(1528204115692).toString()` will print differently depending on the local timezone, but internally it will be the same. For example in Italy it print: `Tue Jun 05 2018 15:08:35 GMT+0200 (ora legale Europa occidentale)`. And probably the method is compensating for the `DateTimeKind`, e.g. `DateTime.Now.Kind`, that saves the type of `DateTime`, if local or utc. – xanatos Jun 05 '18 at 15:38
0

The code below behaves differently for different time zones on users' browsers.

var date = new Date(1528204115692);

You can test it with the same number (millisecond) by changing computers time zone. This code shows different datetimes when changing time zone.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
baris usanmaz
  • 839
  • 1
  • 13
  • 31