将Unix时间戳转换为日期时间字符串 [英] Converting Unix timestamp to Date Time String
问题描述
我想将unix时间戳(类似1537364040000)转换为Date:Time String
I want to convert a unix timestamp (something like this 1537364040000) into a Date:Time String
为此,我做了类似的事情
for this, I did something like this
this.selectedTime = new Date(this.selectedTime*1000);
(我的this.selectedTime之前是unix时间戳)
这应该console.log这样的东西
This should console.log something like this
console.log(this.selectedTime) //Tue May 24 50687 17:30:00 GMT+0530 (India Standard Time)
console.log(typeof this.selectedTime) //Object
在上面,typeof恰好是一个对象,我希望它是字符串,所以我做到了
In the above, The typeof happens to be an object and I wanted it be string so I did this
this.selectedTime = JSON.stringify(this.selectedTime)
是console.loging这样的东西
which is console.logging something like this
console.log(this.selectedTime)
"+050687-06-10T20:40:00.000Z"
[问题:] 有人可以帮我弄清楚我们如何得到这样的东西 Tue May 24 50687 17:30:00 GMT + 0530 或这个 周二5月24日50687 17:30:00 作为字符串吗?
[Question:] Can someone help me in figuring out how can we get something like this Tue May 24 50687 17:30:00 GMT+0530 or this Tue May 24 50687 17:30:00 as a string?
[问题更新:] :也可以有人帮助说出记录的内容,例如 50687 而不是正确的年份(5月24日星期二 50687 17:30:00)
[Question Update:]: Also can someone help in figuring out what it is logging say 50687 instead of proper year (Tue May 24 50687 17:30:00)
我正在使用硬币帽历史记录API http://coincap.io/history/1day/BTC
I am using coincap history API http://coincap.io/history/1day/BTC
推荐答案
您可以在创建时将日期对象设置为字符串:
You can set the date object to string at time of creation:
this.selectedTime = 1537364040000;
this.selectedTime = new Date(this.selectedTime*1000).toString();
console.log(this.selectedTime)
如果您不希望年份为 50687 ,则丢失*1000
If you don't want the year 50687 then lose the *1000
this.selectedTime = 1537364040000;
this.selectedTime = new Date(this.selectedTime).toString(); /* no multiplier */
console.log(this.selectedTime)
这篇关于将Unix时间戳转换为日期时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!