I'm using .NET to generate a JSON file that has many Date
s in it.
For compression, I want to store them as milliseconds since Jan 1, 1970 and not strings, then convert them to Javascript Dates. But .Net's idea of milliseconds since 1970年01月01日 don't match Javascript:
Javascript:
Date.parse("2012-05-15T13:57:57.0000000+00:00")
1337090277000
VB.Net:
Date.Parse("2012-05-15T13:57:57.0000000+00:00").Subtract(New Date(1970,1,1)).TotalMilliseconds
1337101077000.0
The difference is 10800
seconds. The difference at 1970年01月01日 is 0 and changes over time.
Is there a way to compute Javascript's idea of milliseconds-since-epoch from within .Net?
-
The difference is 3 hours exactly. Perhaps the original time was from a different time zone?JConstantine– JConstantine2012年07月28日 13:09:04 +00:00Commented Jul 28, 2012 at 13:09
1 Answer 1
You are comparing apples to oranges.
This is exactly what you would get in javascript as well when in UTC+3 (Israel):
Date.parse("2012-05-15T13:57:57.0000000+00:00") - new Date(1970,1,1)
//1334419077000
This is because when you do new Date
in javascript, that's according to the machine's timezone. It looks like it's the same for vb.net.
You would get the correct number in javascript with:
Date.parse("2012-05-15T13:57:57.0000000+00:00") - Date.UTC(1970,1,1)
//1334411877000
In VB.net
Dim a As Date
a = Date.Parse("2012-05-15T13:57:57.0000000+00:00").ToUniversalTime()
Dim b As Date
b = New Date(1970, 2, 1, 0, 0, 0, DateTimeKind.Utc)
a.Subtract(b).TotalMilliseconds
'1334411877000 Same as javascript
'Note that in javascript, month as 1 is same as 2 in VB.net because months start at 0 in javascript
7 Comments
DateTime.ParseExact( "2012年05月15日T13:57:57.0000000+00:00", "yyyy-MM-ddTHH:mm:ss.fffffffZ",System.Globalization.DateTimeFormatInfo.InvariantCulture)
Date.Parse("2012年05月15日T13:57:57.0000000+00:00").TotalMilliSeconds - New Date(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).TotalMilliseconds
Explore related questions
See similar questions with these tags.