I am working in Python and I have a .csv file that I’ve imported with two columns: a start date and and end date. I need to be able to convert both dates to a readable format in python so that it can calculate how many days between the two days. So an example would be:
Wednesday, 26-Jun-24 02:13:48 UTC
Thursday, 04-Jul-24 03:14:09 UTC (12.54 days ago)
That’s the exact format copied from the .csv file. Can anyone help with this?
I’ve tried importing dateutil and parsing the date but I’m not exactly sure how to use it. Maybe that’s the best way to do it but maybe not – looking for some guidance.
Here is an example of the code I’ve used with no real result:
from dateutil.parser import parse
import datetime
d = 'Monday, 10-Jun-24 17:16:47 UTC'
dt = parse(d)
print(dt.strftime('%m/%d/%y'))
The datetime
module comes with the datetime.strptime
method, used to parse an string into a datetime
object by specifying the format.
Using this table of format codes, the time format Monday, 10-Jun-24 17:16:47 UTC can be parsed as so:
from datetime import datetime
date = "Monday, 10-Jun-24 17:16:47 UTC"
fmt = "%A, %d-%b-%y %H:%M:%S %Z" # refer to the table to understand each code
print(datetime.strptime(date, fmt))
Related StackOverflow question:
- Convert string “Jun 1 2005 1:33PM” into datetime
You can convert your date string using the datetime.strptime()
to parse time in a specific format and datetime.strftime()
to format the time in a specific format like this:
from datetime import datetime
date_str = 'Monday, 10-Jun-24 17:16:47 UTC'
new = datetime.strptime(date_str, "%A, %d-%b-%y %H:%M:%S %Z")
print(new)
2024-06-10 17:16:47
formatted = new.strftime("%m/%d/%y")
print(formatted)
06/10/24
Whilst these
Wednesday, 26-Jun-24 02:13:48 UTC
Thursday, 04-Jul-24 03:14:09 UTC (12.54 days ago)
are not 100% compliant with RFC2822, email.utils.parsedate_to_datetime
seems to cope with them correctly, as long as you drop ago part (where it exists)
import email.utils
datetime1 = email.utils.parsedate_to_datetime("Wednesday, 26-Jun-24 02:13:48 UTC")
datetime2 = email.utils.parsedate_to_datetime("Thursday, 04-Jul-24 03:14:09 UTC ")
print(datetime1) # 2024-06-26 02:13:48+00:00
print(datetime2) # 2024-07-04 03:14:09+00:00
print(datetime1.strftime("%m/%d/%y")) # 06/26/24
print(datetime2.strftime("%m/%d/%y")) # 07/04/24