JonasSH JonasSH -4 years ago 71
Javascript Question

getDay() doesn't work client-side after having sent date to the server

This works perfectly fine on the client-side, at first:



var timeOfMessageSent = new Date();
console.log(timeOfMessageSent); // Mon May 22 2017 14:03:13 GMT+0200 (Romance Summer Time)
var day = timeOfMessageSent.getDay(); // 1
console.log("this is the day: ",day);





However, after having sent the date to the server, and then sent it back to the client, it doesn't work.

Now the date is displayed like this:
2017-05-22T12:03:13.437Z

I guess that's why getDate doesn't work.

How do I make sure that the date is displayed like at first? e.g.
2017-05-22T12:03:13.437Z

Answer Source

Make your server date string to date object.

var timeOfMessageSent = new Date();
console.log(timeOfMessageSent); // Mon May 22 2017 14:03:13 GMT+0200 (Romance Summer Time)
var day = timeOfMessageSent.getDay(); // 1
console.log("this is the day: ",day);

var newDate = new Date("2017-05-25T12:19:55.982Z"); // give your server date and return as date object
var newDay = newDate.getDay();
console.log("this is the new day: ", newDay);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download