Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String to Date format
#3
As poster over strftime().
>>> from datetime import datetime
>>> 
>>> d = 'January 5, 2020'
>>> date_obj = datetime.strptime(d, '%B %d, %Y')
>>> date_obj
datetime.datetime(2020, 1, 5, 0, 0)
>>> print(date_obj)
2020-01-05 00:00:0 
Or simpler use pendulum,advisable to use if need dealing with time zones.
>>> import pendulum
>>> 
>>> d = 'January 5, 2020'
>>> dt = pendulum.parse(d, strict=False)
>>> dt
DateTime(2020, 1, 5, 0, 0, 0, tzinfo=Timezone('UTC'))
>>> print(dt)
2020-01-05T00:00:00+00:00
>>> 
>>> dt.to_date_string()
'2020-01-05'
>>> dt.to_iso8601_string()
'2020-01-05T00:00:00Z'
Reply


Messages In This Thread
String to Date format - by SAF - Apr-06-2021, 12:42 PM
RE: String to Date format - by Axel_Erfurt - Apr-06-2021, 01:17 PM
RE: String to Date format - by snippsat - Apr-06-2021, 02:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Invalid Date Format fo Cached Files jland47 1 154 May-22-2024, 07:04 PM
Last Post: deanhystad
  Compare current date on calendar with date format file name Fioravanti 1 335 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 748 Jan-20-2024, 04:45 AM
Last Post: 1418
  Formatting a date time string read from a csv file DosAtPython 5 1,490 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Set string in custom format korenron 4 1,182 Jan-16-2023, 07:46 PM
Last Post: mutantGOD
  Modifying a date format jehoshua 17 3,192 Oct-29-2022, 08:44 PM
Last Post: jehoshua
  Format String NewPi 2 1,025 Oct-10-2022, 05:50 PM
Last Post: NewPi
  Date format error getting weekday value Aggie64 2 1,484 May-29-2022, 07:04 PM
Last Post: Aggie64
  Convert Date to another format lonesoac0 2 1,716 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Format SAS DATE Racer_x 0 1,030 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