0

I am in a first-year programming class. My prof gave us an assignment and he expected us to have taken this other programming class which wasn't a prerequisite for this one. I never took it so I have no idea how to even remotely do this question and I've looked it up and cannot find anything about it.

So yeah, I need to write a program that should print the date and time of its execution in the following format

Printed on the Dss day of M, Y at HH:MM.

where: D is one or two digits of the day with no leading zeros, ss is one of st, nd, rd, th, whichever appropriate for D, M is the English name of the month, Y is four digits of the year, and HH and MM are the two digits for the hour and the minute respectively, printed with leading zeros

and prints a sample output of:

Printed on the 26th day of July, 2012 at 12:37.

He said you can access the datetime module in Python to write the program. I don't know what that means.

It'd really be appreciated if anyone can lead me in the right direction here. Thanks.

---EDIT---

So I finished the program. Can anyone tell if it it is correct because I don't know the time or date in which the program will get graded and I am not sure whether or not the th,rd,st,or nd condition or the day having no leading zeros condition will work. Thanks.

import time
import datetime
def ss(D):
    D_list = list(D)
    if(D_list[1]==1):
        return "st"
    elif(D_list[1]==2):
        return "nd"
    elif(D_list[1]==3):
        return "rd"
    else:
        return "th"
def main():
    Y = datetime.date.today().strftime("%Y")
    M = datetime.date.today().strftime("%B")
    D = datetime.date.today().strftime("%d")
    HH = datetime.datetime.now().strftime("%I")
    MM = datetime.datetime.now().strftime("%M")  
    print("Printed on the",D.lstrip('0')+ss(D),"day of",M+",",Y,"at",HH+":"+MM+".")
main()

Then the output I get is:

Printed on the 20th day of September, 2013 at 12:39.

Sofia June
  • 169
  • 2
  • 8

1 Answers1

1

You'll want to look at the datetime module. It should have everything you need. For example:

>>> import datetime
>>> mydate = datetime.datetime.now()
>>> print mydate.strftime('Today is %d %B')
Today is 20 September

A list of string formattings for time (eg the '%d' and the '%B') can be found here.

TerryA
  • 58,805
  • 11
  • 114
  • 143