problems playing with dates from any month.

Ian Clark iclark at mail.ewu.edu
Thu Aug 2 14:37:01 EDT 2007


krishnakant Mane wrote:
> On 02/08/07, Ian Clark <iclark at mail.ewu.edu> wrote:
> 
>> http://docs.python.org/lib/node85.html
>>
> I looked there even before.
> but could not figure out what the code did.
> I know in that variable called s there was a string in a valid date format.
> but when datetime.strptime was used, I did not understand the place
> where a date object say d was created.
> I would expect some thing like d = and the function.  but I did not fine that.
> only reference was the datetime module and the documentation is not as good.

 >>> import datetime
 >>> import time
 >>> fmt_string = '29/05/2005'
 >>> date_tuple = time.strptime(fmt_string, '%d/%m/%Y')
 >>> print date_tuple
(2005, 5, 29, 0, 0, 0, 6, 149, -1)
 >>> date_tuple_minus_tz = date_tuple[0:6]
 >>> print date_tuple_minus_tz
(2005, 5, 29, 0, 0, 0)
 >>> date_obj = datetime.datetime(*date_tuple_minus_tz)

This last line is equivalent to the following:
 >>> date_obj = datetime.datetime(2005, 5, 29, 0, 0, 0)
The * in front of a tuple expands it's arguments when making a function call

 >>> print date_obj
2005-05-29 00:00:0
 >>> print date_obj.day == 5
False

> another question I am getting is that where is the list of all
> formatting characters.  like for example Y is 4 digit year M is month
> MM is month in 2 digits etc.
> I am trying to locate a list of all these denoters.
> can you provide me the place?

http://docs.python.org/lib/module-time.html

Look for the strptime() function.

> 
> 
>> Then it's just:
>>
>> if date_obj.day == 5:
>>      print 'It's the fifth day of the month'
>>
> this was much better than the documentation, thanks,
> regards,
> Krishnakant.

Hope that helps.

Ian




More information about the Python-list mailing list