Python Forum
Convert Date to another format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert Date to another format
#1
Hello all,

I am looking to convert the date format of: 2022-03-16 to: 2022-03-16 06:00:00+00:00

How do I do that?

Thank you.
Reply
#2
Could do it with datetime and using strftime() and strptime() Format Codes
I would suggest pendulum ,then it easier and special care has been taken to ensure timezones are handled correctly.
>>> import pendulum
>>> 
>>> d = '2022-03-16'
>>> date = pendulum.parse(d)
>>> date
DateTime(2022, 3, 16, 0, 0, 0, tzinfo=Timezone('UTC'))
>>> print(date)
2022-03-16T00:00:00+00:00
Reply
#3
If you want to get back a valid ISO8601 string, then use datetime.datetime.fromisoformat and datetime.datetime.isoformat.
datetime.datetime.fromisoformat("2022-03-16").replace(hour=6).isoformat()
Output:
'2022-03-16T06:00:00'
To set the tzinfo, you could use the replace method of datetime object.
Using astimezone converts the naive datetime object to a timezone-aware datetime object.

Long version:
import datetime

new_dt = datetime.datetime.fromisoformat("2022-03-16").replace(hour=6, tzinfo=datetime.timezone.utc).isoformat()
print(new_dt)
Output:
2022-03-16T06:00:00+00:00
Cleaned up:
from datetime import datetime as DateTime
from datetime import timezone as TimeZone


def to_iso8601(iso_date: str, hour: int, tzinfo: TimeZone = TimeZone.utc) -> str:
    return (
        DateTime.fromisoformat(iso_date).replace(hour=hour, tzinfo=tzinfo).isoformat()
    )


print(to_iso8601("2020-01-01", 6))
Output:
2020-01-01T06:00:00+00:00
To get rid of the T, you can use str.replace
from datetime import datetime as DateTime
from datetime import timezone as TimeZone


def to_iso(iso_date: str, hour: int, tzinfo: TimeZone = TimeZone.utc) -> str:
    return (
        DateTime.fromisoformat(iso_date)
        .replace(hour=hour, tzinfo=tzinfo)
        .isoformat()
        .replace("T", " ")
    )


print(to_iso("2020-01-01", 6))
Output:
2020-01-01 06:00:00+00:00
For more of this complicated tasks, you should look into https://pypi.org/project/python-dateutil/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 243 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 618 Jan-20-2024, 04:45 AM
Last Post: 1418
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 758 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 2,003 Dec-17-2022, 12:24 AM
Last Post: snippsat
  Modifying a date format jehoshua 17 3,007 Oct-29-2022, 08:44 PM
Last Post: jehoshua
  Convert Json to table format python_student 2 5,552 Sep-28-2022, 12:48 PM
Last Post: python_student
  Convert .xlsx to Format as Table bnadir55 0 894 Aug-11-2022, 06:39 AM
Last Post: bnadir55
  Date format error getting weekday value Aggie64 2 1,427 May-29-2022, 07:04 PM
Last Post: Aggie64
  how to convert and format a varable darktitan 4 1,697 May-29-2022, 12:59 PM
Last Post: darktitan
  Format SAS DATE Racer_x 0 997 Feb-09-2022, 04:44 PM
Last Post: Racer_x

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020