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.

Sales.find({id:id,date:{"$gte":startDate,"$lt":endDate}},function(err, purchases){


Leave a Reply

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