如何将毫秒转换为 Javascript UTC 日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33507289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you convert Milliseconds into a Javascript UTC date?
提问by markthegrea
Given I have the number 1446309338000
, how do I create a JavaScript UTC date?
鉴于我有 number 1446309338000
,如何创建 JavaScript UTC 日期?
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.
new Date(1446309338000)
将等于 CST 时间(中央标准)或本地时间。new Date(Date.UTC(year, month, day, hour, minute, second))
还没有得到这个信息。
Does JavaScript change the time if I do this?
如果我这样做,JavaScript 会改变时间吗?
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).
它是否创建了一个新的 CST 日期,然后将其转换为 UTC?我真的只需要字符串。我从数据库中获取它(来自 Azure 表存储数据库的 RowKey)。
回答by Ulises
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:
如果您有已经是 UTC 日期的毫秒数。这基本上意味着世界时间。现在基于这些毫秒,您可以将 Date 对象转换为您喜欢的字符串:
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!
现在 toString from original Date 和 toUTCString of this new 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"
回答by CommandoScorch
It's actually as simple as homemade biscuits, If you have your date, say:
其实就像自制饼干一样简单,如果你有约会对象,就说:
var date_in_milliseconds = 1504640419000;
You can then initialize a new date like this:
然后,您可以像这样初始化一个新日期:
var human_readable_date = new Date(0); //Date(0) creates a date at the Epoch, so Wed Dec 31 1969
now, just add the milliseconds to the Epoch, and this will give us the desired date:
现在,只需将毫秒添加到 Epoch,这将为我们提供所需的日期:
human_readable_date.setUTCMilliseconds(date_in_milliseconds);
回答by abksrv
Well, if the date string is what you require, hope this helps:
好吧,如果日期字符串是您所需要的,希望这会有所帮助:
new Date(1446309338000).toLocaleString('en-US', {timeZone: 'UTC'})
As far as toISOString()
is concerned, it returns string representation using ISO-8601 standard (the format is: YYYY-MM-DDTHH:mm:ss.sssZ).
toLocaleString()
is human readable format with same result.
就其toISOString()
而言,它使用 ISO-8601 标准返回字符串表示(格式为:YYYY-MM-DDTHH:mm:ss.sssZ)。
toLocaleString()
是具有相同结果的人类可读格式。