Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is output of date function on node.js server wrong?

When date was 2018-03-21 19:40, i tried following code

var date = new Date();
console.log(date);

Output :

2018-03-21T16:40:53.755Z

Server is missing for 3 hours as you see. I fixed it by adding 3 hours but I think it's not a good way. How can i fix this problem with better way ?

like image 702
Kerem Çakır Avatar asked Sep 19 '25 05:09

Kerem Çakır


1 Answers

I don't think the date is incorrect, if you look closely at the format it is being printed, it has a Z at the end, which means:

A suffix which, when applied to a time, denotes a UTC offset of 00:00; often spoken "Zulu" from the ICAO phonetic alphabet representation of the letter "Z".

I guess you are in a place separated by 3 hours from UTC.

Node.js uses this format to print Date objects by default, but you can print your local time using toLocaleString():

console.log(date.toLocaleString());
like image 126
Antonio Val Avatar answered Sep 21 '25 22:09

Antonio Val