0

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);

(My this.selectedTime before this was a unix timestamp)

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

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)

which is console.logging something like this

console.log(this.selectedTime)
"+050687-06-10T20:40:00.000Z"

[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?

[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)

I am using coincap history API http://coincap.io/history/1day/BTC

Community
  • 1
  • 1
Alwaysblue
  • 9,948
  • 38
  • 121
  • 210

2 Answers2

2

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)

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)
StudioTime
  • 22,603
  • 38
  • 120
  • 207
0

You can try one of the following:

console.log(this.selectedTime.toString());
//"Fri Sep 21 2018 09:24:40 GMT+0200 (Mitteleuropäische Sommerzeit)"

console.log(this.selectedTime.toDateString());
//"Fri Sep 21 2018"

console.log(this.selectedTime.toISOString());
//"2018-09-21T07:26:28.793Z"

console.log(this.selectedTime.toGMTString());
//"Fri, 21 Sep 2018 07:27:00 GMT"
Franz Deschler
  • 2,456
  • 5
  • 25
  • 39
  • 1
    This answer would be better if it showed an example of the output, and perhaps a little explanation as to why this would help. – Danny Staple Sep 20 '18 at 14:48