anaghapramesh
anaghapramesh

Reputation: 87

Error - function missing required argument 'month' (pos 2), is displayed when trying to fetch time in fixed format

I am trying to fetch the time in the following format, but I'm getting error that function is missing required argument for 'month'. It would be a great help if someone could resolve this small confusion.

import datetime
startTimeList = ['drwxr-xr-x  3 plan 4096 Mar 21 02:00 file_listener', 'ksh', '010001', '0\n']
startTimeStr = int(startTimeList[2])
print(startTimeStr)
startTimeStr = datetime.datetime(startTimeStr)
startTime = startTimeStr.strftime("%I:%M:%S %p")
print(startTime)

Expected Output:

010001
01:00:01 AM

Error Displayed:

function missing required argument 'month' (pos 2)

Upvotes: 1

Views: 10383

Answers (1)

mkrieger1
mkrieger1

Reputation: 23310

Converting startTimeList[2] to an integer was not helping you, as you needed to convert it back to a string again.

datetime.datetime expects integer arguments year, month, day, which is not what you wanted to do.

You meant to use datetime.datetime.strptime to parse the string '010001' into a datetime.datetime object, which you can then convert to a differently formatted string with strftime again:

import datetime

startTimeList = ['drwxr-xr-x  3 plan 4096 Mar 21 02:00 file_listener', 'ksh', '010001', '0\n']
startTimeStr = startTimeList[2]
print(startTimeStr)

startTime = datetime.datetime.strptime(startTimeStr, "%H%M%S")
startTimeResult = startTime.strftime("%I:%M:%S %p")
print(startTimeResult)

It gives you 01:00:01 AM however, since it has no way of knowing that 010001 means a pm time.

Upvotes: 1

Related Questions