1

I am given a list of time in military hours and minutes as a string. Naturally, I need to convert each string into a datetime object using the strptime class with the %H for military or 24-hour clock and %M for minutes. However, anytime I try to parse it I am getting an output for year,month,day hour.minute.seconds for no reason

ie.

from datetime import datetime
string_time = '18:00'
formatted_time = datetime.strptime(string_time, '%H:%M')

print(formatted_time)

output:

1900-01-01 18:00:00

Just this morning when I was working on the assignment it printed perfectly fine but out of nowhere I started getting this weird output. Nowhere in my directory or current file did I ever try to format a year-month-day time. I did however try to create an empty datetime object but scrapped it after figuring out you can't have an empty datetime object.

1 Answers1

1

Explanation

A datetime object, as its name implies, contains a date and a time. The reason your datetime object has a date 1900-01-01 even though you only specify the time is because the date of the datetime object cannot be empty (1900-01-01 is the default date when no date is provided).

Solution

If you don't need to handle the "date", simply ignore the date part of your datetime object. If you need to print out the time, do yourDateTimeObject.strftime('%H:%M').

Tyzeron
  • 186
  • 2