Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

azure table storage - How do you convert Milliseconds into a Javascript UTC date?

Given I have the number 1446309338000, how do I create a JavaScript UTC date?

new Date(1446309338000) will equal a CST time (central standard) or local time.
new Date(Date.UTC(year, month, day, hour, minute, second)) haven't got this info yet.

Does JavaScript change the time if I do this?

new Date(1446309338000).ISOString();

Is it creating a new CST date and then converting it to UTC? I really just need the string. I am taking it from a database (RowKey from a Azure Table storage database).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you have the milliseconds that's already the UTC date. Which basically means the universal time. Now based on those millis you can convert the Date object into a String of your like:

new Date(1446309338000).toUTCString() // timezone free universal format
> "Sat, 31 Oct 2015 16:35:38 GMT"
new Date(1446309338000).toString() // browser local timezon string
> "Sat Oct 31 2015 09:35:38 GMT-0700 (PDT)"
new Date(1446309338000).toISOString() // ISO format of the UTC time
> "2015-10-31T16:35:38.000Z"

Now, if for some reason (I can't see a valid reason, but just for the heck of it) you're looking for having a different amount of milliseconds that represent a different date but that would print the same in the local browser timezone, you can do this calculation:

new Date(1446309338000 - new Date(1446309338000).getTimezoneOffset() * 60 * 1000))

Now toString from original Date and toUTCString of this new Date would read the same up to the Timezone information, because of course they're not the same date!

new Date(1446309338000).toString()
> "Sat Oct 31 2015 09:35:38 GMT-0700 (PDT)"
new Date(1446309338000 - new Date(1446309338000).getTimezoneOffset() * 60 * 1000).toUTCString()
> "Sat, 31 Oct 2015 09:35:38 GMT"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...