Comparing Two Timestamp Strings In Python
I have JSON file which has timestamps in it.The timestamp in the format shown below. timestamp1 Fri Mar 27 15:25:24 NZDT 2015 timestamp2 Fri Mar 27 15:23:01 NZDT 2015 Between th
Solution 1:
You need to convert your timestamp strings into datetime
objects. Normally, I'd recommend that you use the strptime
. However, you have a timezone in your string (NZDT
) and the documentation says:
Support for the %Z directive is based on the values contained in tzname and whether daylight is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).
Instead, we'll utilize the python-dateutilpackage.
pip install python-dateutil
From here, we can utilize the parse function to get a datetime object. With this, you can perform any comparisons you need.
>>>from dateutil.parser import parse>>>t1 = "Fri Mar 27 15:25:24 NZDT 2015">>>t2 = "Fri Mar 27 15:23:01 NZDT 2015">>>d1 = parse(t1)>>>d2 = parse(t2)>>>d1 > d2
True
Post a Comment for "Comparing Two Timestamp Strings In Python"