0

I have a variable

date_1 = "2023-04-14T09:57:40-04:00"

how to convert to proper format - 2023-04-14T05:57:40Z

Expected ouput - "2023-04-14T05:57:40Z"

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
pp45
  • 17
  • 5
  • Sorry, what 'type' of time is `04:00` at the end? Is it a minute? Hour? I'm not sure. Also what does the Z represent? I need it to provide an adequate answer. Thanks! – Anony Mous Apr 17 '23 at 11:33
  • its utc timezone – pp45 Apr 17 '23 at 11:36
  • 1
    @AnonyMous - See [RFC 3339](https://www.rfc-editor.org/rfc/rfc3339) / [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) – Matt Johnson-Pint Apr 18 '23 at 01:36
  • 1
    @pp45 - Note, you've done the conversion in the wrong direction in your example. `-04:00` means 4 hours _behind_ UTC. Thus the UTC hour is `13` (not `5`), because `13 - 4 = 9`. – Matt Johnson-Pint Apr 18 '23 at 01:38

2 Answers2

4

Essentially, you're looking to convert a date/time string with a UTC offset to UTC. You can do that like

from datetime import datetime, timezone

date_1 = "2023-04-14T09:57:40-04:00"
utc = datetime.fromisoformat(date_1).astimezone(timezone.utc)

print(utc)
# 2023-04-14 13:57:40+00:00

print(utc.isoformat().replace("+00:00", "Z"))
# 2023-04-14T13:57:40Z

Notes:

  • a UTC offset like -04:00 is not a time zone to be precise; multiple time zones can share the same UTC offset at a given time
  • as Matt Johnson-Pint commented, UTC offsets are subtracted from the give time to get UTC, so 9 am with -4 hours offset becomes 1 pm UTC
  • Python currently does not offer you to represent UTC as 'Z' when formatting to string, thus the replace
  • datetime module docs
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
0

Please refer to the other answer posted, this one does not properly convert UTC.

import datetime
import calendar
date_1 = '2023-04-14T09:57:40-04:00'
datex, timez=date_1[0:-6], date_1[-6:]
timed = datetime.datetime.strptime(datex, "%Y-%m-%dT%H:%M:%S")
timez = datetime.datetime.strptime(timez.replace(':',''), "%z")
output=datetime.datetime.fromtimestamp(calendar.timegm(timed.timetuple()), tz=timez.tzinfo).strftime("%Y-%m-%dT%H:%M:%SZ")

Code first splits datetime (datex) and timezone (timez). Converts datex to datetime.datetime then epoch, and convert timez to datetime. Finally converting datex to UTC time based off current timezone, then formats the datetime.datetime object to string.

Previous answer (not working)

import datetime
import calendar
date_1 = "2023-04-14T09:57:40-04:00"
timed = datetime.datetime.strptime(date_1, "%Y-%m-%dT%H:%M:%S%z")
output=datetime.datetime.fromtimestamp(calendar.timegm(timed.timetuple()), tz=timed.tzinfo).strftime("%Y-%m-%dT%H:%M:%SZ")

It's a bit long, but it should work! The program first converts the date_1 to a datetime.datetime object, then it converts it to epoch time, from which it converts this epoch time to the UTC timezone, and finally formats the epoch time to the output string.

Please let me know if this works for you.

Anony Mous
  • 345
  • 3
  • 10
  • Getting error - ValueError: time data '2023-04-14T09:57:40-04:00' does not match format '%Y-%m-%dT%H:%M:%S%z' – pp45 Apr 17 '23 at 12:17
  • I have a solution here - https://stackoverflow.com/questions/74768770/how-to-convert-datetime-with-timezone-to-utc-datetime But its not working - AttributeError: type object 'datetime.datetime' has no attribute 'fromisoformat' – pp45 Apr 17 '23 at 12:18
  • @pp45 Give my edited answer a shot. – Anony Mous Apr 17 '23 at 12:35