[BangPypers] date/time difference between two dates

Saju Pillai saju.pillai at gmail.com
Thu Feb 19 12:09:26 CET 2009


LOhit wrote:
> Hello All,
> 
> I am parsing a log file to extract data for the last one week from the 
> current date. The log file does not record the year, only month, day of 
> the month and time.
> 
> My question is, the date is in "Month day-of-the-month time" format in 
> the log file (ex. "Nov 22 15:15:42") and the current date I get from 
> "datetime" module is in ISO format. How do I convert the date in log 
> file to ISO format(or any other format) and then compare with the 
> current date/time.

Look at time.strptime() or datetime.strptime().

This method takes the string to be parsed and the format of the string.

For eg:

time_object = time.strptime("Nov 22 15:15:42", "%b %d %H:%M:%S")

This will give you a time object on which you can call strftime(format, 
time_object) where format is any format you want.

In your particular case, the year will default to 1900, you may want to 
explicitly supply the year like strptime("2008 Nov 22 15:15:42", "%Y %b 
%d %H:%M:%S").


srp
-- 
http://saju.net.in


More information about the BangPypers mailing list