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 8064401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:21:09+00:00 2026-06-05T11:21:09+00:00

Is there a simple way to remove the timezone from a pytz datetime object?

  • 0

Is there a simple way to remove the timezone from a pytz datetime object?
e.g. reconstructing dt from dt_tz in this example:

>>> import datetime
>>> import pytz
>>> dt = datetime.datetime.now()
>>> dt
datetime.datetime(2012, 6, 8, 9, 27, 32, 601000)
>>> dt_tz = pytz.utc.localize(dt)
>>> dt_tz
datetime.datetime(2012, 6, 8, 9, 27, 32, 601000, tzinfo=<UTC>)
  • 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-06-05T11:21:11+00:00Added an answer on June 5, 2026 at 11:21 am

    To remove a timezone (tzinfo) from a datetime object:

    # dt_tz is a datetime.datetime object
    dt = dt_tz.replace(tzinfo=None)
    

    If you are using a library like arrow, then you can remove timezone by simply converting an arrow object to to a datetime object, then doing the same thing as the example above.

    # <Arrow [2014-10-09T10:56:09.347444-07:00]>
    arrowObj = arrow.get('2014-10-09T10:56:09.347444-07:00')
    
    # datetime.datetime(2014, 10, 9, 10, 56, 9, 347444, tzinfo=tzoffset(None, -25200))
    tmpDatetime = arrowObj.datetime
    
    # datetime.datetime(2014, 10, 9, 10, 56, 9, 347444)
    tmpDatetime = tmpDatetime.replace(tzinfo=None)
    

    Why would you do this? One example is that mysql does not support timezones with its DATETIME type. So using ORM’s like sqlalchemy will simply remove the timezone when you give it a datetime.datetime object to insert into the database. The solution is to convert your datetime.datetime object to UTC (so everything in your database is UTC since it can’t specify timezone) then either insert it into the database (where the timezone is removed anyway) or remove it yourself. Also note that you cannot compare datetime.datetime objects where one is timezone aware and another is timezone naive.

    ##############################################################################
    # MySQL example! where MySQL doesn't support timezones with its DATETIME type!
    ##############################################################################
    
    arrowObj = arrow.get('2014-10-09T10:56:09.347444-07:00')
    
    arrowDt = arrowObj.to("utc").datetime
    
    # inserts datetime.datetime(2014, 10, 9, 17, 56, 9, 347444, tzinfo=tzutc())
    insertIntoMysqlDatabase(arrowDt)
    
    # returns datetime.datetime(2014, 10, 9, 17, 56, 9, 347444)
    dbDatetimeNoTz = getFromMysqlDatabase()
    
    # cannot compare timzeone aware and timezone naive
    dbDatetimeNoTz == arrowDt # False, or TypeError on python versions before 3.3
    
    # compare datetimes that are both aware or both naive work however
    dbDatetimeNoTz == arrowDt.replace(tzinfo=None) # True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there any way to remove an attachment from Zend_Mail object? Simple example: $mail
Is there a simple way to remove the tr:hover style from twitter bootstrap such
Is there a simple way to remove the video-stream from a IMediaWriter? I have
Is there a simple way to remove the same line of text from a
Is there a simple way to remove all characters from a given string that
is there a simple way to remove the namespace from the XML root element.
Is there a simple way to work with C++ objects directly from C? I
Is there a simple way to discard/remove the last result in a queryset without
Is there a simple way to use jQuery to remove all background styles on
Is there a simple way to remove duplicates in the following basic query: email_list

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.