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
Answer from user1094786 on Stack Overflowc# - Remove Time Zone Offset from DateTimeOffset? - Stack Overflow
c# - Convert datetime without timezone - Stack Overflow
python - Remove timezone information from datetime object - Stack Overflow
Removing the time zone from datetime field in QGIS - Geographic Information Systems Stack Exchange
Videos
You can use the DateTime property of DateTimeOffset.
Example:
string s = "2013-07-22T08:51:38.000-07:00";
var dateTimeOffset =DateTimeOffset.Parse(s, null);
Console.WriteLine(dateTimeOffset.DateTime);
Outputs:
22/07/2013 08:51:38
you can try this.
DateTimeOffset.Parse("2013-07-22T08:51:38.000-07:00").DateTime.ToString("dd-MM-yyyy hh:mm:ss tt");
For those that came here for the actual question 'Remove timezone information from datetime object', the answer would be something like:
datetimeObject.replace(tzinfo=None)
from datetime import datetime, timezone
import time
datetimeObject = datetime.fromtimestamp(time.time(), timezone.utc)
print(datetimeObject)
datetimeObject = datetimeObject.replace(tzinfo=None)
print(datetimeObject)
use strftime if you already have a datetime object
dt.strftime('%Y-%m-%d %H:%M:%S')
If you need a datetime object from a string, the fastest way is to use strptime and a slice:
st = '2016-12-14 15:57:16.140645'
dt = datetime.strptime(st[:19], '%Y-%m-%d %H:%M:%S')
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.
After inspecting provided GeoPackage I could not find the problem in any of your datetime fields, specifically "Save_times1" and "Loc1".


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

Before:

After:

So i have a script that pulls some stuff from Sharepoint. One of the fields is the timestamp that an entry was made into the Sharepoint list. When pulling it from the API it is returned in the following format:
| ENTERED_TM |
|---|
| 2024-02-28T17:48:43Z |
The datatype of this field when returned from the API is Object. I want to drop the time zone stuff so i just have YYYY-MM-DD H:M:S.
I've tried a couple different things:
First I convert it to datetime with cleaned_df["ENTERED_TM"] = pd.to_datetime(cleaned_df["ENTERED_TM"])
Then i've tried 2 ways of dropping the time zone cleaned_df["ENTERED_TM"].dt.tz_convert(None) and cleaned_df["ENTERED_TM"].dt.tz_localize(None)
Both of those leave me this:
| ENTERED_TM |
|---|
| 2024-02-28 17:48:43+00:00 |
Any help would be appreciated
Getting the Date part of a DateTime object didn't workout for me because I'm working on the client-side and the returned web service values have some null dates. As a result, it tries to get the Date part of a null value and it throws a runtime exception. The following example is how I solved my problem:
string dt = employer.BirthDay.ToString();
if(dt == ""){ dt = "N/A";}
else dt = dt.Substring(0,10);
- Get the DateTime value as string into a string variable.
- Check if it's null. If null, assign a string variable.
- If not null, get the first 10 characters of the string
DateTimevalue and assign it to the string variable.
I'm sharing this for future reference.
Try this, if you use a DateTimeOffset, it will also take care of the timezone
date1 = date1.LocalDateTime.Date;
If you need to add hours, use this:
date1 = date1.LocalDateTime.Date;
date1 = date1.AddHours(23).AddMinutes(59).AddSeconds(59);