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 extract only time from datetime?

By
supreeth
on 10/23/15, 4:13 AM 943 views

I have datetime field called field1 there i am selecting date & time and i have another field called filed2 there it should display only fields1's time. How can we do this?

As of now i am displaying both date and time. Unable to display only time.


Code:

start_date: fields.datetime('Start Date')
start_time: fields.datetime()
start = datetime.strptime(start_date, "%d-%m-%Y %H:%M:%S") 
value['start_time'] = start.strftime("%d-%m-%Y %H:%M:%S")

This will give o/p in datetime format.I need only time.


Thanks

1

Serpent Consulting Services Pvt. Ltd.

--Serpent Consulting Services Pvt. Ltd.--
4341
| 6 6 8
Gandhinagar, India
--Serpent Consulting Services Pvt. Ltd.--

Serpent Consulting Services Pvt. Ltd. Your Odoo/OpenERP Solution, just an email away!

Serpent Consulting Services Pvt. Ltd.
On 10/23/15, 5:21 AM

Hello Supreeth,

You have to convert the datetime of string into datetime format and then convert it into string by extracting time only:

from datetime import datetime

start_dt = datetime.strptime(start_date, "%d-%m-%Y %H:%M:%S") # convert into datetime fromat
value['start_time'] = datetime.strftime(start_dt, "%H:%M:%S") # convert into string format and extract time only



1
Chandni
On 10/23/15, 5:18 AM

Hello Supreeth,

You can get time by:

        datetime.datetime.now().time()

 It will give you current time.

Thank You.

Thanks Chandni. It's works for me.

Jignesh Mehta
on 10/23/15, 5:19 AM
0
supreeth
On 10/23/15, 5:59 AM

Thank you all,

Got Solution,

start = datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S")
user = self.pool.get('res.users').browse(cr, uid, uid)
tz = pytz.timezone(user.tz) if user.tz else pytz.utc
ran = pytz.utc.localize(start).astimezone(tz)
value['start_time'] = ran.strftime("%I:%M")

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/23/15, 4:13 AM
Seen: 943 times
Last updated: 10/23/15, 5:59 AM