Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Thossaporn Itngam
Thossaporn Itngam
2,348 Points

Why time in node and chrome are different

i use node with express and import morgan for logging.

this log was showed UTC and when i new date with node i got UTC. both of these are wrong time for me because i want local timezone.

about "new Date()" in chrome and node ( in terminal ) I dont know why i declare date between these are different

My timezone is '+07:00' assume today is 2017-07-24 00:00:00

In console tab of chrome ( the result show time is correct )

var today = new Date()
console.log(today) // Mon Jul 24 2017 00:00:00 GMT+0700 (+07)

In terminal ( the result show wrong time, it show UTC )

var today = new Date()
console.log(today) // 2017-07-23T17:00:00.000Z

Should i set default timezone in node ?

1 Answer

Steven Parker
Steven Parker
231,269 Points

It seems like you answered your own question.

You pointed out that Node shows the time in UTC by default, but you said that you "want local timezone."

So the answer to "Should i set default timezone in node ?" would be "Yes". :smile:
As an alternative, you could set it explicitly in your program.

Thossaporn Itngam
Thossaporn Itngam
2,348 Points

lol .... if yes, how to set default timezone ? or we have another way to solve this ?

Steven Parker
Steven Parker
231,269 Points

On doing some research, I'm not finding an option to make Node use the local timezone by default. But you can always have it take it into account when converting the date to a string:

console.log(today.toLocaleTimeString());
Thossaporn Itngam
Thossaporn Itngam
2,348 Points

Now, i solved this with momentjs