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.

1

How to convert datetime to date in python

By
Drees Far
on 10/28/15, 11:53 AM 6,623 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
4

Akhil P Sivan

--Akhil P Sivan--
3423
| 5 3 6
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

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

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


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

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: 10/28/15, 11:53 AM
Seen: 6623 times
Last updated: 11/21/15, 6:09 AM