5 examples of 'python compare datetimes' in Python

Every line of 'python compare datetimes' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
734def cmp_datetimes(first, second):
735 """Compare two datetime strings and sort by most recent.
736
737 :param first: A string containing a datetime
738 :param second: A string containing a datetime
739 :returns: -1 if ``first`` is more recent, 1 if ``second`` is more recent, or 0
740 if they are equivalent.
741 """
742 parsed_first = _safe_parse_datetime(first)
743 parsed_second = _safe_parse_datetime(second)
744 if parsed_first > parsed_second:
745 return -1
746 elif parsed_first == parsed_second:
747 return 0
748 else:
749 return 1
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

15def test_http_date_python(self):
16 format_date_time(time())
47def gt(dt_str):
48 dt, _, us = dt_str.partition(".")
49 dt = datetime.strptime(dt, "%Y-%m-%dT%H:%M:%S")
50 us = int(us.rstrip("Z"), 10)
51 return dt + timedelta(microseconds=us)
41def iso_sort(tup):
42 dummy, data = tup
43 return iso2date(data['duration']['start'])
195def _get_date_for_comparison(self):
196 '''
197 Function for avoiding duplication of code lines
198 '''
199 return transform_in_date(self.year, self.month, self.day)

Related snippets