-1

Pretty straightforward issue, but I haven't found any information on this after looking around a bunch.

Essentially, I want to convert a series of UTC dates (e.g. "1505952000") into regular date strings (e.g., "9/21"), to use today as an example.

For some reason, however, .toDateString() is erroring out as "not a function" when I try to run it. How do I make this simple conversion work?

Here's my code, and I've console-logged day.dt to ensure that it's a valid UTC date when it runs:

let dt = day.dt.toDateString();
yoursweater
  • 1,883
  • 3
  • 19
  • 41

1 Answers1

1

UTC var stored in seconds from Jan. 1, 1970.

So to convert it back to the local date time, use this snippet:

var d = new Date(0);
d.setUTCSeconds(1505952000);

console.log(d);

OR

var d = new Date(1505952000 * 1000); // Because this constructor takes miliseconds.

console.log(d);
Duc Filan
  • 6,769
  • 3
  • 21
  • 26