Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7034651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:12:43+00:00 2026-05-28T01:12:43+00:00

I have a rather basic question. the date time for an entry in a

  • 0

I have a rather basic question.
the date time for an entry in a collection is saved as

    "lastUpdated": ISODate("2011-12-07T02:46:51.101Z")

which is in GMT format. How do I query the entry so that the query output i get is in EST format?
is this possible in the query itself or do I have to manually subtract 5 hrs (ESt = -5.00 hrs)?
The query i used is;

    db.Collection.find({Type: 'Reports', patId: 'JOHNSONGARY'}, 
                       {'lastUpdated': 1} )

EDIT:
I use python for querying and am using the returned timestamp as such;

    str(mongo_documents['lastUpdated'].strftime('%Y-%m-%d %H:%M:%S'))

How do i deduct 5 hours in this command?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:12:44+00:00Added an answer on May 28, 2026 at 1:12 am

    Check out the documentation – datetime objects returned by pymongo always represent a time in UTC, just as dates stored in MongoDB are always stored as (that is, assumed to be in) UTC

    pymongo can convert your datetimes automatically to be time zone aware if you set the tz_info flag to True when creating your Connection. You can then use datetime.astimezone() method to convert to another time zone if you wish.

    So for example you could use pytz for timezones or if you only need EST write your own:

    import datetime
    
    class Eastern(datetime.tzinfo):
    
        def utcoffset(self, dt):
          return datetime.timedelta(hours=-5)
    
        def tzname(self, dt): 
            return "EST"
    
        def dst(self, dt):
            return datetime.timedelta(0)
    
    
    EST = Eastern()
    

    Then you can do this:

    # Get now for EST
    now = datetime.datetime.now(EST)
    print now.strftime('%Y-%m-%d %H:%M:%S')
    
    from pymongo import Connection
    # Create a timezone aware connection
    connection = Connection('localhost', 27017, tz_aware=True)
    
    # Save your data
    db = connection.test_database
    db.stackoverflow.save({"Type": "reports", "patId": 'JOHNSONGARY', "lastUpdated": now})
    
    doc = db.stackoverflow.find()[0]
    print doc['lastUpdated'].astimezone(EST).strftime('%Y-%m-%d %H:%M:%S')
    
    # Confirm they are the same
    assert doc['lastUpdated'].astimezone(EST).strftime('%Y-%m-%d %H:%M:%S') == now.strftime('%Y-%m-%d %H:%M:%S')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have rather very basic question. I have one GUI class and another which
I'm relatively new to SVN and have a rather basic question. I have several
So I have a rather basic javascript problem which I have been slamming my
I'm learning jQuery/javascript and have a rather basic question. Why doesn't this work? Thanks
I have a rather basic and general question about atomicity of UPDATE ... SET
I have a rather basic question. I have several(5) UIImageView images on my screen.
Beware, this is a rather basic question (I think). I have a UserControl, called
this is a rather basic java question I have an array containing String that
I have a very basic question: Why did Twitter choose Scala rather than Groovy
Rather basic question here guys. Basically I have code like this: public SuperPanel() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.