[Tutor] how to use the time module?

Dick Moores rdm at rcblue.com
Fri Nov 5 15:22:31 CET 2004


I just discovered that calendar.weekday() is usable back to year 1 
instead of just 1900.
"the datetime strftime() methods require year >= 1900"

 >>> from calendar import weekday
 >>> import string, datetime
 >>> print weekday(1,1,1) # doesn't need to be 0001,01,01
0
 >>> print weekday(1000,1,1) # doesn't need to be 1000,01,01
2
 >>> print weekday(1776,7,4) # ditto
3
 >>> print weekday(9999,12,31)
4

Dick

At 02:11 11/5/2004, Kent Johnson wrote:
>datetime.date objects support strftime() and weekday() directly:
> >>> import datetime
> >>> a=datetime.date(2004, 11, 5)
> >>> a.strftime('%A')
>'Friday'
> >>> a.weekday()
>4
>
>So your code can be a little simpler
>import string
>import datetime
>
>y,m,d=string.split(raw_input("Enter a date('YYYY-MM-DD'):"),"-")
>a=datetime.date(int(y),int(m),int(d))
>print a.strftime('%A')
>
>Kent
>
>At 01:47 AM 11/5/2004 -0800, Dick Moores wrote:
>>OK, so you want to input a date and get that date's weekday? I don't 
>>know how to do that with the time module, but you can use the calendar 
>>module.
>>
>>Try:
>>
>>import string
>>import datetime
>>from calendar import weekday
>>
>>y,m,d=string.split(raw_input("Enter a date('YYYY-MM-DD'):"),"-")
>>a=datetime.date(int(y),int(m),int(d))
>>weekday = weekday(int(y), int(m), int(d))
>>print "%s was a %s" % (a, weekday)
>>
>>You'll want to write a function that converts the integer to the 
>>corresponding weekday.
>>
>> From the calendar module doc:
>>weekday( year, month, day)
>>Returns the day of the week (0 is Monday) for year (1970-...), month 
>>(1-12), day (1-31).
>>
>>In your code, you reassigned a to strftime("%A"), so of course a 
>>becomes today's weekday.
>>
>>At 00:17 11/5/2004, Lin Jin wrote:
>>>thx for your reply,but seems i still have some problem with my 
>>>program,my code is like this:
>>>
>>>import string
>>>import datetime
>>>import time
>>>y,m,d=string.split(raw_input("Enter a date('YYYY-MM-DD'):"),"-")
>>>a=datetime.date(int(y),int(m),int(d))
>>>from time import strftime
>>>a=strftime("%A")
>>>print "Today is "+a
>>>
>>>you can see that,my a variable is the user input year,month,and 
>>>day.but if i implement it like this,it seems it just give out the 
>>>weekday of today.what's wrong with my code?
>>>
>>>>From: Dick Moores <rdm at rcblue.com>
>>>>To: "Lin Jin" <jinlin555 at msn.com>,tutor at python.org
>>>>Subject: Re: [Tutor] how to use the time module?
>>>>Date: Thu, 04 Nov 2004 23:46:07 -0800
>>>>
>>>>At 22:38 11/4/2004, Lin Jin wrote:
>>>>>i am new to python.i want to know that,if i have a variable a,and i 
>>>>>want to use the time module,the strftime method to output the 
>>>>>weekday,how could i do that? i know that i should use the the 
>>>>>"%A",but if i write a code like this:
>>>>>>>>time.strftime("%A",a).
>>>>>it is not working.anyone can help me solve this one?
>>>>
>>>>This should do it (with Python 2.1 or later):
>>>>
>>>> >>> from time import strftime
>>>> >>> a = strftime("%A")
>>>> >>> a
>>>>'Thursday'
>>>>
>>>>Dick Moores
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list