javascript 从 unix 时间戳到日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16978331/
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
from unix timestamp to datetime
提问by Robin Wieruch
I have something like /Date(1370001284000+0200)/
as timestamp. I guess it is a unix date, isn't it? How can I convert this to a date like this: 31.05.2013 13:54:44
我有类似/Date(1370001284000+0200)/
时间戳的东西。我想这是一个unix日期,不是吗?如何将其转换为这样的日期:31.05.2013 13:54:44
I tried THISconverter for 1370001284 and it gives the right date. So it is in seconds.
我为 1370001284尝试了这个转换器,它给出了正确的日期。所以它是在几秒钟内。
But I still get the wrong date for:
但我仍然得到错误的日期:
var substring = unix_timestamp.replace("/Date(", "");
substring = substring.replace("000+0200)/", "");
var date = new Date();
date.setSeconds(substring);
return date;
回答by Dai
Note my use of
t.format
comes from using Moment.js, it is not part of JavaScript's standardDate
prototype.
注意我的使用
t.format
来自使用Moment.js,它不是 JavaScript 标准Date
原型的一部分。
A Unix timestamp is the number of seconds since 1970-01-01 00:00:00 UTC.
Unix 时间戳是自 1970-01-01 00:00:00 UTC 以来的秒数。
The presence of the +0200
means the numeric string is not a Unix timestamp as it contains timezone adjustment information. You need to handle that separately.
存在+0200
意味着数字字符串不是 Unix 时间戳,因为它包含时区调整信息。您需要单独处理。
If your timestamp string is in milliseconds, then you can use the milliseconds constructor and Moment.jsto format the date into a string:
如果您的时间戳字符串以毫秒为单位,那么您可以使用毫秒构造函数和Moment.js将日期格式化为字符串:
var t = new Date( 1370001284000 );
var formatted = t.format("dd.mm.yyyy hh:MM:ss");
If your timestamp string is in seconds, then use setSeconds
:
如果您的时间戳字符串以秒为单位,则使用setSeconds
:
var t = new Date();
t.setSeconds( 1370001284 );
var formatted = t.format("dd.mm.yyyy hh:MM:ss");
回答by Blair Anderson
Looks like you might want the ISO format so that you can retain the timezone.
看起来您可能需要 ISO 格式,以便您可以保留时区。
var dateTime = new Date(1370001284000);
dateTime.toISOString(); // Returns "2013-05-31T11:54:44.000Z"
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
回答by Rauli Rajande
Without moment.js:
没有moment.js:
var time_to_show = 1509968436; // unix timestamp in seconds
var t = new Date(time_to_show * 1000);
var formatted = ('0' + t.getHours()).slice(-2) + ':' + ('0' + t.getMinutes()).slice(-2);
document.write(formatted);
回答by Akshay Haryani
Import moment js:
导入时刻js:
var fulldate = new Date(1370001284000);
var converted_date = moment(fulldate).format(");
回答by Tommi Komulainen
The /Date(ms + timezone)/
is a ASP.NET syntax for JSON dates. You might want to use a library like momentjsfor parsing such dates. It would come in handy if you need to manipulate or print the dates any time later.
这/Date(ms + timezone)/
是 JSON 日期的 ASP.NET 语法。您可能想使用像momentjs这样的库来解析此类日期。如果您以后需要操作或打印日期,它会派上用场。
回答by taggartJ
If using react:
如果使用反应:
import Moment from 'react-moment';
Moment.globalFormat = 'D MMM YYYY';
then:
然后:
<td><Moment unix>{1370001284}</Moment></td>
回答by J C
I would like to add that Using the library momentjsin javascript you can have the whole data information in an object with:
我想补充一点,在 javascript中使用库momentjs您可以在一个对象中拥有整个数据信息:
const today = moment(1557697070824.94).toObject();
You should obtain an object with this properties:
您应该获得具有以下属性的对象:
today: {
date: 15,
hours: 2,
milliseconds: 207,
minutes: 31,
months: 4
seconds: 22,
years: 2019
}
It is very useful when you have to calculate dates.
当您必须计算日期时,它非常有用。