Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String to Date format
#1
Hi

I have a date column as below. the date is in this format "January 5, 2020"
I want this date in to mm/dd/YYY format -- 01/05/2020. How I can convert using python?

Date
January 5, 2020
January 10, 2022
January 12, 2020
Reply
#2
https://www.w3schools.com/python/python_datetime.asp
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 228 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 605 Jan-20-2024, 04:45 AM
Last Post: 1418
  Formatting a date time string read from a csv file DosAtPython 5 1,281 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Set string in custom format korenron 4 1,089 Jan-16-2023, 07:46 PM
Last Post: mutantGOD
  Modifying a date format jehoshua 17 2,978 Oct-29-2022, 08:44 PM
Last Post: jehoshua
  Format String NewPi 2 943 Oct-10-2022, 05:50 PM
Last Post: NewPi
  Date format error getting weekday value Aggie64 2 1,423 May-29-2022, 07:04 PM
Last Post: Aggie64
  Convert Date to another format lonesoac0 2 1,672 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Format SAS DATE Racer_x 0 996 Feb-09-2022, 04:44 PM
Last Post: Racer_x
  TypeError: not enough arguments for format string MaartenRo 6 2,929 Jan-09-2022, 06:46 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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