Datetime Video Formatting

After watching the YouTube on Datetime in the Modules section thought I’d give it a try myself. But I keep returning errors when I try to parse the date into a suitable format.

For example I enter the year, month and day to return 1954 Jan 15 and the error returned is:

ValueError("'%s' is a bad directive in format '%s'" %
ValueError: '-' is a bad directive in format ' %Y %b %-d'

I think it might be the day but my selection as I used range doesn’t have the zero padding so the docs show I need %-d. Most things say that flag is a Linux specific one which is fine that’s what I’m running right now.

from datetime import datetime
import inquirer

print('What is your year of birth')
year = int(input())

if year < 1800:
    print('Records aren\'t avaiable this far back' )
    exit()

monthList = [
    inquirer.List(
        'option', 
        message = 'What is your month of birth', 
        choices = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
        carousel = True
    ),
]

month = inquirer.prompt(monthList)

mon = month.get('option')

dayList = [
    inquirer.List(
        'option', 
        message = 'What is your day of birth', 
        choices = list(range(1,32)),
        carousel = True
    ),
]

day = inquirer.prompt(dayList)

day = day.get('option')

date = str(f'{year} {mon} {day}')
print(date)

formatDate = '%Y %b %-d'

parsedDate = datetime.strptime('{date}', formatDate)
print(parsedDate)

Thanks for any light that could be shed.

the error states the issue. You cannot use a “-”

wouldn’t something like this work?

x = datetime.datetime(2022, 6, 12)
  
print(x.strftime("%Y %b %d"))

I don’t see anything about a flag in the documentation. Perhaps I missed something?
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

This one of the website with the filter for a day without a leading zero.

I think I might try and find a method of getting the date list with leading zero then it should work.

from datetime  import datetime


date = '1951-02-02'
formatDate = '%Y-%m-%d'

parsedDate = datetime.strptime('{date}', '{formatDate}')
print(parsedDate)

It still reported the same error so have stripped all the manual dates out for testing but the outcome is the same. Used the codes here but it reports:

ValueError: time data '{date}' does not match format '{formatDate}'

Unless I’m totally misunderstanding I told it to format 4 digit year leading zero month and leading zero day.

Just incase anyone else has similar issue it was the variable in the parsedDate, although the data was a string I need to format the variable in the datetime.strptime call.

Full code below:

from datetime  import datetime
import key_file
import inquirer

nasaKey = key_file.MY_NASA_API_KEY

print('What is your year of birth')
year = int(input())

if year < 1900:
    print('Records aren\'t avaiable this far back' )
    exit()

monthList = [
    inquirer.List(
        'option', 
        message = 'What is your month of birth', 
        choices = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
        carousel = True
    ),
]

month = inquirer.prompt(monthList)

mon = month.get('option')

dayList = [
    inquirer.List(
        'option', 
        message = 'What is your day of birth', 
        choices = list(range(1,32)),
        carousel = True
    ),
]

day = inquirer.prompt(dayList)

day = str(day.get('option'))

day = day.rjust(2, '0')

date = str(f'{year}-{mon}-{day}')
print(date)

parsedDate = datetime.strptime(f'{date}', '%Y-%b-%d')
print(parsedDate)
1 Like