Algorithms, Computer Science and Programming Puzzles

Mongodb and Node.js Timezone Problems with Date Objects

When you start working with Node/Mongodb you probably will resort to the native Date object to play around with and store dates. I did it too, only to discover that you will face a lot of timezone related problems if you do that.

Mongodb stores dates on the UTC timezone as default, but your Node app will create Date objects using your local timezone.

Most apps don’t need date granularity at hour level, only day, so dealing with timezone issues is crazy.

A better approach? Store your dates as a string using the ‘YYYY-MM-DD’ format. It will be much simpler, and you will still be able to query your DB filtering by date, since string comparison will work perfectly.

Post navigation

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

About The Author

My name is Daniel Scocco, and I am a programmer and entrepreneur located in Brazil. On this website you'll find my hobby programming projects, code samples I find interesting and solutions to programming puzzles and challenges I come across.