Odoo Help


How to convert datetime to date in python

Drees Far
on 10/28/15, 11:53 AM 8,863 views


Please, i am using a datetime field. And, i want to convert my datetime field to a date field.

here is what i have tried:


import time

import datetime

from datetime import datetime, date, time 

b = 2015-10-28 16:09:59

d = datetime.now().strftime('%Y-%m-%d')

print d

and i got an error which is :

b = 2015-10-28 16:22:31.515561


SyntaxError: invalid syntax

Is here any idea.


Unrelevant to Odoo/OpenERP itself, use `.date()` as suggested by zbik.

Marvin Taboada
on 10/28/15, 11:43 PM

Akhil P Sivan

--Akhil P Sivan--
Akhil P Sivan
| 5 3 7
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 10/28/15, 1:29 PM


Try like this:

import dateutil.parser
b = "2015-10-28 16:09:59"
d = dateutil.parser.parse(b).date()
print d

will give output as 2015-10-28

Excellent Akhil, +1

Ankit H Gandhi(AHG)
on 11/21/15, 9:46 AM


on 9/29/16, 4:10 AM


| 6 4 7
Lodz, Poland

On 10/28/15, 12:04 PM

You probably need brackets:

b = '2015-10-28 16:09:59'

UPDATE, example:

>>> import datetime
>>> datetime.datetime.now().date()
datetime.date(2015, 10, 28)
>>> tmp = '2015-10-28 16:09:59'
>>> dt = datetime.datetime.strptime(tmp,'%Y-%m-%d %H:%M:%S')
>>> dt.date()
datetime.date(2015, 10, 28)
>>> dd = dt.date()
>>> print dd

thanks a lot for your answer but what it doesnt work. can you help me know how to convert a datetime to date in python please

Drees Far
on 10/28/15, 12:21 PM

hey @zbik would you please help me :/

Drees Far
on 10/28/15, 12:38 PM

Answer updated.

on 10/28/15, 12:48 PM

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 platform 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.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 10/28/15, 11:53 AM
Seen: 8863 times
Last updated: 11/21/15, 6:09 AM