Sha*_*tle 253 javascript date utc epoch
我现在一直在与此斗争.我正在尝试将纪元转换为日期对象.这个时代以UTC的形式发送给我.每当你通过new Date()
一个时代,它就会认为它是本地的时代.我尝试创建一个UTC对象,然后使用setTime()
它调整到适当的纪元,但唯一有用的方法是toUTCString()
和字符串不能帮助我.如果我将该字符串传递给新的日期,它应该注意到它是UTC,但事实并非如此.
new Date( new Date().toUTCString() ).toLocaleString()
Run Code Online (Sandbox Code Playgroud)
我的下一次尝试是尝试获取当地当前时期和UTC当前时期之间的差异,但我也无法得到它.
new Date( new Date().toUTCString() ).getTime() - new Date().getTime()
Run Code Online (Sandbox Code Playgroud)
它只给了我很小的差异,低于1000,这是毫秒.
有什么建议?
小智 432
我想我有一个更简单的解决方案 - 将初始日期设置为纪元并添加UTC单位.假设您有一个UTC纪元var存储在几秒钟内.如何1234567890
.要将其转换为本地时区的正确日期:
var utcSeconds = 1234567890;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(utcSeconds);
Run Code Online (Sandbox Code Playgroud)
d
现在是一个日期(在我的时区)设置为 Fri Feb 13 2009 18:31:30 GMT-0500 (EST)
dje*_*lin 211
它很容易,new Date()
只需要几毫秒,例如
new Date(1394104654000)
> Thu Mar 06 2014 06:17:34 GMT-0500 (EST)
Run Code Online (Sandbox Code Playgroud)
Gus*_*Gus 29
而对于日志,我使用Moment.js库来完成此操作,无论如何我都使用它来进行格式化.
moment.utc(1234567890000).local()
>Fri Feb 13 2009 19:01:30 GMT-0430 (VET)
Run Code Online (Sandbox Code Playgroud)
log*_*ic8 17
纪元时间是从1970年1月1日开始的秒数.从1970年1月1日起date.getTime()
返回毫秒数.所以..如果你有一个纪元时间戳,请将它转换为javascript时间戳乘以1000.
function epochToJsDate(ts){
// ts = epoch timestamp
// returns date obj
return new Date(ts*1000);
}
function jsDateToEpoch(d){
// d = javascript date obj
// returns epoch timestamp
return (d.getTime()-d.getMilliseconds())/1000;
}
Run Code Online (Sandbox Code Playgroud)
小智 16
function ToLocalDate (inDate) {
var date = new Date();
date.setTime(inDate.valueOf() - 60000 * inDate.getTimezoneOffset());
return date;
}
Run Code Online (Sandbox Code Playgroud)
ALD*_*ENT 10
var myDate = new Date( your epoch date *1000);
来源 - https://www.epochconverter.com/programming/#javascript
Epoch time (i.e. Unix Epoch time) is nearly always the number of seconds that have expired since 1st Jan 1970 00:00:00 (UTC time), not the number of milliseconds which some of the answers here have implied.
https://en.wikipedia.org/wiki/Unix_time
Therefore, if you have been given a Unix Epoch time value it will probably be in seconds, and will look something like 1547035195
. If you want to make this human readable in JavaScript, you need to convert the value to milliseconds, and pass that value into the Date(value)
constructor, e.g.:
const unixEpochTimeMS = 1547035195 * 1000;
const d = new Date(unixEpochTimeMS);
// Careful, the string output here can vary by implementation...
const strDate = d.toLocaleString();
Run Code Online (Sandbox Code Playgroud)
您不需要执行已d.setUTCMilliseconds(0)
接受答案中的步骤,因为 JavaScriptDate(value)
构造函数采用以毫秒为单位的UTC值(不是本地时间)。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Syntax
另请注意,您应该避免使用Date(...)
采用字符串日期时间表示的构造函数,这是不推荐的(请参阅上面的链接)。
小智 6
最简单的方法
如果你有以毫秒为单位的unix纪元,就我而言 -1601209912824
const dateObject = new Date(milliseconds)
const humanDateFormat = dateObject.toString()
Run Code Online (Sandbox Code Playgroud)
Sun Sep 27 2020 18:01:52 GMT+0530 (India Standard Time)
Run Code Online (Sandbox Code Playgroud)
Sun Sep 27 2020 18:01:52 GMT+0530 (India Standard Time)
Run Code Online (Sandbox Code Playgroud)
以 [ms] 为单位将当前纪元时间转换为 24 小时制。您可能需要指定禁用 12 小时格式的选项。
$ node.exe -e "var date = new Date(Date.now()); console.log(date.toLocaleString('en-GB', { hour12:false } ));"
2/7/2018, 19:35:24
Run Code Online (Sandbox Code Playgroud)
或作为 JS:
var date = new Date(Date.now());
console.log(date.toLocaleString('en-GB', { hour12:false } ));
// 2/7/2018, 19:35:24
console.log(date.toLocaleString('en-GB', { hour:'numeric', minute:'numeric', second:'numeric', hour12:false } ));
// 19:35:24
Run Code Online (Sandbox Code Playgroud)
注意:en-GB
这里的使用,只是一个(随机)选择一个使用 24 小时格式的地方,它不是你的时区!
除了@djechlin 的上述答案之外
d = '1394104654000';
new Date(parseInt(d));
Run Code Online (Sandbox Code Playgroud)
将 EPOCH 时间转换为人类可读的日期。只是不要忘记 EPOCH 时间类型必须是整数。
小智 5
我发现的最简单的解决方案是:
var timestamp = Date.now(), // returns milliseconds since epoch time
normalisedTime = new Date(timestamp);
Run Code Online (Sandbox Code Playgroud)
* 1000
请注意,语句末尾没有 the ,new Date(timestamp)
因为(无论如何对我来说!)似乎总是给出错误的日期,即不是给出 2019 年,而是给出年份 51015,所以请记住这一点。
归档时间: |
|
查看次数: |
323664 次 |
最近记录: |