1

I convert a date to a number by running

Date.parse(doc.createdAt);

sO i GET THIS:

1576699528000

which probably turns it into a string as its getting passed from the server to the front end and then back to the server

How do I turn it back into a date object?

stackers
  • 2,701
  • 4
  • 34
  • 66
  • 1
    well I get a number of seconds. then i pass it to the front end and then pass it back and need to convert it back – stackers Dec 18 '19 at 20:06
  • this question was incorrectly closed – stackers Dec 18 '19 at 20:57
  • How do the duplicates not answer your question? You want to know how to turn a time value into a Date, they answer that (with 29 different answers to choose from, which include those given here). – RobG Dec 19 '19 at 00:34

2 Answers2

2

Just use constructor of Date:

let mls = Date.parse('Wed, 09 Aug 1995 00:00:00 GMT');
let date = new Date(mls );

An example

let mls = Date.parse('Wed, 09 Aug 1995 00:00:00 GMT');
let date = new Date(mls);
console.log(date);
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • 1
    OMG. I have been going in circles on this for hours. Somehow your answer was the only one which clicked.... Specifically, if you type `let date = Date(mls);` then it will return the time now (ie useless), but if you type `let data = new Date(mls);` then it works. – Tunneller Apr 04 '22 at 04:25
1

Try using Date.prototype.setTime() if you have a date object to shift over or just pass it to the constructor, ex. new Date(num)

iPhoenix
  • 719
  • 7
  • 20