在node.js中将整数转换为Date [英] Convert integer to Date in node.js
问题描述
我正在尝试使用节点.js和日期将整数转换为日期。
I'm trying to convert a integer to a Date using node .js, and Date.
我知道这是一个非常常见的问题,但所有解决方案之前发布的内容未能帮助我。
I know that this is a very common question but all the solutions that have been posted before have failed to help me.
我从 http://api.guardian.gg/chart/elo/4611686018432537994 ,
示例日期:1461110400000
Example date: 1461110400000
我尝试了什么:
var date = String(new Date(elodata.x));
和
var date = String(new Date(parseInt(elodata.x)));
但结果是我的日期无效。
But I get invalid date as a result.
我意识到这可能不可行,因为我不知道guardian.gg如何处理这些数据。但你永远不知道。
I realise that this might not be doable because I don't know how guardian.gg handles this data. But you never know.
推荐答案
你可以直接将你的价值传递给日期
Javascript中的构造函数,如果它是一个整数(它似乎在:
You can pass in your value directly to a Date
constructor in Javascript if it is an integer (which it appears to be in :
var date = new Date(elodata.x);
同样,您也可以使用 setTime()
如果您已有现有对象,则在Javascript中运行您的整数值:
Likewise, you can also use the the setTime()
function in Javascript to pass your integer value in if you already have an existing object :
var date = new Date();
d.setTime(elodata.x);
示例
var d1 = new Date(1461110400000);
console.log(`Constructor: ${d1}`);
var d2 = new Date();
d2.setTime(1461110400000);
console.log(`setTime(): ${d2}`);
这篇关于在node.js中将整数转换为Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!