Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to compare two dates ? (problem)

By
Tatorus
on 3/9/16, 10:47 AM 531 views

Hi, i wanna compare two dates with this line but an error occurs :

if idProd[0].last_synchro_ingram < date:

     print "blabla"

This is the print of my two dates (they have the same format) :

date = 2016-03-02

idProd[0].last_synchro_ingram = 2016-03-09


1

Jérémy Kersten (jke)

--Jérémy Kersten (jke)--
2993
| 3 2 5
Jodoigne, Belgium
--Jérémy Kersten (jke)--

Jérémy is a member of the core R&D team since september 2013. He developed several projects for OpenERP version 8 including the eCommerce, the Google Calendar synchronization and the new product variants/configurator. Now, he manages Odoo website and themes for saas.

Jérémy Kersten (jke)
On 3/9/16, 2:26 PM

date from Odoo are string.

use datetime.strptime(your_date_as_string, DEFAULT_SERVER_DATE_FORMAT)

@jke I tested with it and it didn't work : DEFAULT_SERVER_DATE_FORMAT = "%Y-%m-%d" if datetime.strptime(idProd[0].last_synchro_ing, DEFAULT_SERVER_DATE_FORMAT) datetime.strptime(date, DEFAULT_SERVER_DATE_FORMAT): print "idProd[0].last_synchro_ing > date"

Tatorus
on 3/10/16, 4:12 AM
0
Tatorus
On 3/10/16, 3:53 AM

@jke

I tested with it and it didn't work :

DEFAULT_SERVER_DATE_FORMAT = "%Y-%m-%d"
if datetime.strptime(idProd[0].last_synchro_ing, DEFAULT_SERVER_DATE_FORMAT) < datetime.strptime(date, DEFAULT_SERVER_DATE_FORMAT):

    print "idProd[0].last_synchro_ing < date"

if datetime.strptime(idProd[0].last_synchro_ing, DEFAULT_SERVER_DATE_FORMAT) > datetime.strptime(date, DEFAULT_SERVER_DATE_FORMAT):

    print "idProd[0].last_synchro_ing > date"

are you sure that last_synchro_ing is a date and not a datetime. Best way to debug is to set a psb in your code and check at fly the strptime

Jérémy Kersten (jke)
on 3/10/16, 4:35 AM

date (2016-03-03) is idProd[0].last_synchro_ing (2016-03-10) is So, I try to convert the string 'last_synchro_ing' to a date with : strp1 = datetime.strptime("idProd[0].last_synchro_ingram", "%Y-%m-%d") But it didn't work.

Tatorus
on 3/10/16, 4:46 AM

date (2016-03-03) is datetime.date idProd[0].last_synchro_ing (2016-03-10) is str So, I try to convert the string 'last_synchro_ing' to a date with : strp1 = datetime.strptime("idProd[0].last_synchro_ingram", "%Y-%m-%d") But it didn't work.

Tatorus
on 3/10/16, 4:47 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 3/9/16, 10:47 AM
Seen: 531 times
Last updated: 3/11/16, 4:08 AM