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

  • Home
  • SEARCH
  • 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 101557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:47:38+00:00 2026-05-11T00:47:38+00:00

The best I can come up with for now is this monstrosity: >>> datetime.utcnow()

  • 0

The best I can come up with for now is this monstrosity:

>>> datetime.utcnow() \ ...   .replace(tzinfo=pytz.UTC) \ ...   .astimezone(pytz.timezone('Australia/Melbourne')) \ ...   .replace(hour=0,minute=0,second=0,microsecond=0) \ ...   .astimezone(pytz.UTC) \ ...   .replace(tzinfo=None) datetime.datetime(2008, 12, 16, 13, 0) 

I.e., in English, get the current time (in UTC), convert it to some other timezone, set the time to midnight, then convert back to UTC.

I’m not just using now() or localtime() as that would use the server’s timezone, not the user’s timezone.

I can’t help feeling I’m missing something, any ideas?

  • 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. 2026-05-11T00:47:38+00:00Added an answer on May 11, 2026 at 12:47 am

    I think you can shave off a few method calls if you do it like this:

    >>> from datetime import datetime >>> datetime.now(pytz.timezone('Australia/Melbourne')) \             .replace(hour=0, minute=0, second=0, microsecond=0) \             .astimezone(pytz.utc) 

    BUT… there is a bigger problem than aesthetics in your code: it will give the wrong result on the day of the switch to or from Daylight Saving Time.

    The reason for this is that neither the datetime constructors nor replace() take DST changes into account.

    For example:

    >>> now = datetime(2012, 4, 1, 5, 0, 0, 0, tzinfo=pytz.timezone('Australia/Melbourne')) >>> print now 2012-04-01 05:00:00+10:00 >>> print now.replace(hour=0) 2012-04-01 00:00:00+10:00 # wrong! midnight was at 2012-04-01 00:00:00+11:00 >>> print datetime(2012, 3, 1, 0, 0, 0, 0, tzinfo=tz) 2012-03-01 00:00:00+10:00 # wrong again! 

    However, the documentation for tz.localize() states:

    This method should be used to construct localtimes, rather than passing a tzinfo argument to a datetime constructor.

    Thus, your problem is solved like so:

    >>> import pytz >>> from datetime import datetime, date, time  >>> tz = pytz.timezone('Australia/Melbourne') >>> the_date = date(2012, 4, 1) # use date.today() here  >>> midnight_without_tzinfo = datetime.combine(the_date, time()) >>> print midnight_without_tzinfo 2012-04-01 00:00:00  >>> midnight_with_tzinfo = tz.localize(midnight_without_tzinfo) >>> print midnight_with_tzinfo 2012-04-01 00:00:00+11:00  >>> print midnight_with_tzinfo.astimezone(pytz.utc) 2012-03-31 13:00:00+00:00 

    No guarantees for dates before 1582, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll try to explain this the best I can. I have a drop down
I will try and word this question the best I can, for it is
I'm going to try to explain this best I can I will provide more
I can't best describe this in words, so I'll show you with pictures. Here's
The best way I can think of to ask this is by example... In
As this question is hard to describe, that's the best title i could come
Sorry, that's the best subject I can come up with, if I understood the
Any ideas of how best i can implement article revision history for a Java
I will try and set the scene as best i can. *lights candle What
I have a file server and wanting to optimize it the best I can,

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.