johnjohn1
johnjohn1

Reputation: 55

Conversion of date string to Date object

When I use

var today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate());

I get that today is Tue Jan 14 00:00:00 GMT+02:00 2020

If I do today.toDateString() then I get Tue Jan 14 2020

If I store the last result to a cell how can I convert it back to what it was? (Date object)

Upvotes: 1

Views: 46

Answers (2)

Alessio Cantarella
Alessio Cantarella

Reputation: 5201

Yes, there are 4 ways to create a new Date object in JavaScript:

  1. new Date()
  2. new Date(year, month, day, hours, minutes, seconds, milliseconds)
  3. new Date(milliseconds)
  4. new Date(date string)

The last one solves your problem:

let now = new Date();
let today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
console.log(today);
let today2 = new Date(today.toString());
console.log(today2);

Upvotes: 1

Gangadhar Gandi
Gangadhar Gandi

Reputation: 2252

Try like below,

let today = new Date();
let todayInString = today.toDateString();
let originalDateForm = new Date(todayInString);
console.log(originalDateForm)

Upvotes: 1

Related Questions