Print out a date, given year, month, and day as numbers : Date Time « Development « Python






Print out a date, given year, month, and day as numbers

Print out a date, given year, month, and day as numbers

months = [
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December'
]

# A list with one ending for each number from 1 to 31
endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
        + ['st', 'nd', 'rd'] + 7 * ['th'] \
        + ['st']

year    = raw_input('Year: ')
month   = raw_input('Month (1-12): ')
day     = raw_input('Day (1-31): ')

# Remember to subtract 1 from month and day to get a correct index
month_name = months[int(month)-1]
ordinal = day + endings[int(day)-1]

print month_name + ' ' + ordinal + ', ' + year

           
       








Related examples in the same category

1.Use time structureUse time structure
2.Dates are easily constructed and formattedDates are easily constructed and formatted
3.Dates support calendar arithmeticDates support calendar arithmetic
4.Define your own class to store time and date
5.Define class to use system time class