query mongodb according to different timezone

12,510 views
Skip to first unread message

SHUBHAM SAINI

unread,
Jul 4, 2016, 2:11:54 PM7/4/16
to mongodb-user
As we all know mongodb saves time in utc format so if you query on any date field then result will be based on utc time format BUT i want to to query on a date field based on est timezone. How can i achieve this ?

Chris Cunningham

unread,
Jul 14, 2016, 3:36:13 AM7/14/16
to mongodb-user

Shubham,

In order to query by another timezone, you have to shift the ISODate by specifying the “time offset” as described in ISO 8601.

For example:

db.collection.find({ date: { $gte: ISODate('2015-01-01T00:00:00Z'), $lte: ISODate('2016-12-31T23:59:59Z') } })

Would be changed to:

db.collection.find({ date: { $gte: ISODate('2015-01-01T00:00:00-04:00'), $lte: ISODate('2016-12-31T23:59:59-04:00') } })

By tacking on offset hours -04:00 to the date, it offsets the timezone in the query to the corresponding Eastern Daylight Time value as per the ISO 8601 document mentioned above. Of course you would have to modify this to -05:00 when Eastern Standard Time is in effect.

If you need to manipulate dates with time zones, you may want to consider loading a helper into the mongo shell using the load() command (or by adding JavaScript to your .mongorc.js startup file).

For example, Moment.js and Moment Timezone should be compatible with the mongo shell:

load(‘/path/to/moment.min.js’)
load(‘/path/to/moment.timezone.min.js’)

If you have a requirement to use an unmodified local time value in your application, you might consider storing the local time zone alongside the UTC timestamp as described in the Model Time Data documentation.

If you run into any problems creating a query by timezone, please provide an example of the query and document you are trying to use.

Regards,

Chris


Tim Hawkins

unread,
Jul 14, 2016, 3:40:23 AM7/14/16
to mongodb-user

Convert from est to utc,  query,  convert results back to est.


On 5 Jul 2016 2:11 a.m., "SHUBHAM SAINI" <shub.s...@gmail.com> wrote:
As we all know mongodb saves time in utc format so if you query on any date field then result will be based on utc time format BUT i want to to query on a date field based on est timezone. How can i achieve this ?

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/12db0721-dc44-42cd-b018-495012e72738%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ajeet Mohan

unread,
Feb 10, 2017, 4:06:03 PM2/10/17
to mongodb-user
How would you convert to EST for all entries of "date" upon querying?

Ajeet Mohan

unread,
Feb 13, 2017, 11:33:03 AM2/13/17
to mongodb-user
Chris,

How would I convert to EST for all entries of "date" upon querying? The example you gave is for one entry. Yet I want to take all of the dates stored in the "date" key and convert it to EST. Would you know how to do that?

Kevin Adistambha

unread,
Feb 23, 2017, 1:43:18 AM2/23/17
to mongodb-user

Hi,

How would I convert to EST for all entries of “date” upon querying? The example you gave is for one entry. Yet I want to take all of the dates stored in the “date” key and convert it to EST. Would you know how to do that?

I believe you are asking about this in a separate thread as well: https://groups.google.com/forum/#!topic/mongodb-user/60_GOy56jB4. Let’s keep the discussion in the new thread rather than extending this old thread.

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages