date and time comparison how to

MRAB python at mrabarnett.plus.com
Mon Oct 29 23:30:27 EDT 2012


On 2012-10-30 03:11, Dave Angel wrote:
> On 10/29/2012 10:13 PM, noydb wrote:
>> I guess I get there eventually!
>> This seems to work
>>
>>     pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.path.getmtime(pdf)))
>>     intermediateTime = time.strptime(pdf_timeStamp, "%m%d%y%H%M%S")
>>     pdfFile_compareTime = time.mktime(intermediateTime)
>>
>> (and I'll do the same to the user entered date-n-time and then compare)
>>
>>
>> Lastly, so can anyone chime in and tell me if this is a good method or not?  Is there a better way?
>
> Please read the rest of the thread in particular the message 3 hours ago
> from Gary Herron
>
>                  import datetime, os, stat
>                  mtime = os.lstat(filename)[stat.ST_MTIME]   // the files
> modification time
>                  dt = datetime.datetime.fromtimestamp(mtime)
>
> Now you can compare two datetimes simply by
>     if dt1 < dt2:
>
> Or you can subtract them, and examine the difference.
>
> What's the need for all that string conversion stuff?
>
+1

Incidentally, the best order for dates is year (4 digits - remember
Y2K? :-)) then month then day.



More information about the Python-list mailing list