Remove timezone from datetime python

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

How can I get rid of the time zone in the date/time field?

So I have a GeoPackage with the following data:

Remove timezone from datetime python

and want to get just 2021-06-07 16:25:40

asked Dec 6, 2021 at 13:29

4

In my QGIS (3.22), when I use datetime format, I get timezone information included by default. You can convert it to a string to get rid of it - use this expression, where datetime is the name of the field containing the datetime-information:

left(to_string(datetime), 19)

However, you can't use the resulting string field as a datetime field any more. Including timezone makes sense - as 2021-12-06 14:48:41 is not the same everywhere on Earth.

Remove timezone from datetime python

answered Dec 6, 2021 at 13:48

Remove timezone from datetime python

BabelBabel

44.8k7 gold badges45 silver badges132 bronze badges

2

After inspecting provided GeoPackage I could not find the problem in any of your datetime fields, specifically "Save_times1" and "Loc1".

Remove timezone from datetime python

Remove timezone from datetime python


Proceed with RMC > Properties > Attributes Forms > Fields (see documentation) where change the Field Format from ISO Date Time into Date Time.

Remove timezone from datetime python

Before:

Remove timezone from datetime python

After:

Remove timezone from datetime python

answered Dec 6, 2021 at 14:18

Remove timezone from datetime python

7

You need to go to the field calculator. Then tick update existing field. Select your desired field and type:

regexp_replace( "date" , '(MSK)', '')

Then hit OK. Good Luck.

answered Dec 6, 2021 at 13:54

Remove timezone from datetime python

AshAsh

1,1086 silver badges21 bronze badges

4

How do I remove the timezone from a datetime column in Python?

tz_localize(None) method can be applied to the dataframe column to remove the timezone information.

How do I remove the timezone from a date in python?

To remove timestamp, tzinfo has to be set None when calling replace() function. First, create a DateTime object with current time using datetime. now(). The DateTime object was then modified to contain the timezone information as well using the timezone.

How do you convert UTC to time in python?

Getting the UTC timestamp Use the datetime. datetime. now() to get the current date and time. Then use tzinfo class to convert our datetime to UTC.

What is Astimezone in Python?

The astimezone() function is used to return a DateTime instance according to the specified time zone parameter tz. Note: The returned DateTime instance is having the new UTC offset value as per the tz parameter.