comparing 2 dates in python did not work as expected

  • Last Update :
  • Techknowledgy :

I am trying to compare a date I extracted from a csv file to the current date, to check if the difference is bigger than seven days. Here is my code:

with open(path) as csvfile:
   readcsv = csv.reader(csvfile, delimiter = ',')
for row in readcsv:
   iso_ts = str(row[3])
datum = (datetime.datetime.strptime(''.join(iso_ts.rsplit(':', 1)), '%Y-%m-%dT%H:%M:%S%z'))

current_time = (datetime.datetime.strptime(datetime.datetime.now(), '%Y-%m-%dT%H:%M:%S%z'))

Without even comparing these I get the following error

File "./netapp.py", line 32, in <module>
   current_time = (datetime.datetime.strptime(datetime.datetime.now(),'%Y-%m-%dT%H:%M:%S%z'))
   TypeError: must be str, not datetime.datetime

Suggestion : 2

Last Updated : 24 May, 2018

Output :

d1 is greater than d2: False
d1 is less than d2: True
d1 is not equal to d2: True

Suggestion : 3

To check that a module’s docstrings are up-to-date by verifying that all interactive examples still work as documented.,To perform regression testing by verifying that interactive examples from a test file or a test object work as expected.,The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown. There are several common ways to use doctest:,DocTestFinder: Finds all docstrings in a given module, and uses a DocTestParser to create a DocTest from every docstring that contains interactive examples.

""
"
This is the "example"
module.

The example module supplies one
function, factorial().For example,

   >>>
   factorial(5)
120
   ""
"

def factorial(n):
   ""
"Return the factorial of n, an exact integer >= 0.

>>>
[factorial(n) for n in range(6)]
[1, 1, 2, 6, 24, 120] >>>
factorial(30)
265252859812191058636308480000000
   >>>
   factorial(-1)
Traceback(most recent call last):
   ...
   ValueError: n must be >= 0

Factorials of floats are OK, but the float must be an exact integer:
   >>>
   factorial(30.1)
Traceback(most recent call last):
   ...
   ValueError: n must be exact integer >>>
   factorial(30.0)
265252859812191058636308480000000

It must also not be ridiculously large:
   >>>
   factorial(1e100)
Traceback(most recent call last):
   ...
   OverflowError: n too large ""
"

import math
if not n >= 0:
   raise ValueError("n must be >= 0")
if math.floor(n) != n:
   raise ValueError("n must be exact integer")
if n + 1 == n: #
catch a value like 1e300
raise OverflowError("n too large")
result = 1
factor = 2
while factor <= n:
   result *= factor
factor += 1
return result

if __name__ == "__main__":
   import doctest
doctest.testmod()
$ python example.py
$
$ python example.py - v
Trying:
   factorial(5)
Expecting:
   120
ok
Trying: [factorial(n) for n in range(6)]
Expecting: [1, 1, 2, 6, 24, 120]
ok
Trying:
   factorial(1e100)
Expecting:
   Traceback(most recent call last):
   ...
   OverflowError: n too large
ok
2 items passed all tests:
   1 tests in __main__
8 tests in __main__.factorial
9 tests in 2 items.
9 passed and 0 failed.
Test passed.
$
if __name__ == "__main__":
   import doctest
doctest.testmod()
python M.py

Suggestion : 4

The problem is that I am unable to compare both fields. I get no error and the "if statement" is not read.,I solved the problem by converting "rec.date" to string in order to be able to compare both values "as strings", since "vals.get('date')" is already a string. It solved my problem.,Dear all,I am having problems on comparing 2 dates in Odoo 12.I have a variable that contains a date. If I print it I get:,data = vals.get('date') #string

Dear all,
I am having problems on comparing 2 dates in Odoo 12.
I have a variable that contains a date. If I print it I get:

>> print(date) 2019 - 05 - 11

I also have a field of date type with a calender widget set on xml.

So, when I print it's value I get:

>> print vals.get('date') 2019 - 05 - 11

The problem is that I am unable to compare both fields. I get no error and the "if statement" is not read.

if date == vals.get('date'): print("Date is equal")

Here you are comparing two strings where odoo retrieves date or DateTime fields as a string. So here you are comparing str == str and results vary according to that. The solution is that you have to typecast the fields before comparison using pythons default or odoo's way. I can give you an example.

date = vals.get('date') # String
date = vals.get('date') # String
date = feilds.Date.from_string(date) # Date(01, 02, 2019) dateobjectOutput: -