Odoo Help

1

How to convert datetime to date in python

By
Drees Far
on 10/28/15, 11:53 AM 17,125 views

Hello!!

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:

Python:

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.

Regards

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

Marvin Taboada
on 10/28/15, 11:43 PM
5
Akhil P Sivan
On 10/28/15, 1:29 PM

Hi,

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

Excellent

yasmina
on 9/29/16, 4:10 AM
4
Zbik
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
2015-10-28






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.

Zbik
on 10/28/15, 12:48 PM

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.

Register

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)

Stats

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