Extract only time (HH:MM) from datetime field - Odoo 10 - python

I'a m trying to extract only time (Hour:Minute) from datetime field
Example:
today_with_hour = fields.Datetime(
string=u'hora',
default=fields.Datetime.now,
)
I would like to know how get only hour from today_with_hour in format
17:10:20

This is one way to extract:
from datetime import datetime
now = datetime.now()
print(str(now.hour)+':'+str(now.minute)+':'+str(now.second))

This may be better way to do it
You can use strftime
Example:
from datetime import datetime
datetime.now().strftime("%H:%M:%S")
In your case you can follow like this:
from datetime import datetime
datetime.strptime('20/06/2019 17:28:52', "%d/%m/%Y %H:%M:%S").time()
Output will be:
17:28:52

Better way to do is by using strftime().
dt = datetime.strptime('20/06/2019 17:28:52', "%d/%m/%Y %H:%M:%S")
dt.strftime("%H:%M:%S")
output:
17:28:52
To get the current time from the datetime.now()
datetime.datetime.now().time().strftime("%H:%M:%S")
O/P:
'11:16:17'
if you want to get the time with milliseconds also, use isoformat()
datetime.datetime.now().time().isoformat()
O/P:
'11:20:34.978272'

Related

Convert datetime object into a string

I need to convert a datetime into a string using numpy.
Is there another way to directly convert only one object to string that doesn't involve using the following function passing an array of 1 element (which returns an array too)?
numpy.datetime_as_string(arr, unit=None, timezone='naive', casting='same_kind')
With this function, I can make the conversion, but I just want to know if there is a more direct/clean way to do it.
Thanks in advance.
As we dont know what is inside of arr, I assume it is just datetime.now()
If so try this:
import datetime
datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
>>> '2022-07-28 10:27:34.986848'
If you need numpy version:
np.array(datetime.datetime.now(), dtype='datetime64[s]')
>>> array('2022-07-28T10:32:19', dtype='datetime64[s]')
if you just want to convert one numpy DateTime64 object into a string, here is the answer.
import datetime
yourdt = yourdt.astype(datetime.datetime)
yourdt_str = yourdt.strftime("%Y-%m-%d %H:%M:%S")
that's it
from datetime import datetime
now = datetime.now() # current date and time
year = now.strftime("%Y")
print("year:", year)
month = now.strftime("%m")
print("month:", month)
day = now.strftime("%d")
print("day:", day)
time = now.strftime("%H:%M:%S")
print("time:", time)
date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
print("date and time:",date_time)

How to convert my date format to another format

My Date for mat is below
import time
timestr = time.strftime("%Y%m%d-%H%M%S")
My out >> 20200607-080127
I need timestr above to convert to 07/06/2020 08:01:27
I need to use timestr only to convert
Please, use the following:
>>> import datetime
>>> timestr = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
>>> timestr
'07/06/2020 17:08:31'
datetime is recommended over time because there are a few formatting strings that are not supported by time, but work just find with datetime.
You can specify every format you want, so for your desired output use:
time.strftime("%d/%m/%Y %H:%M:%S")
You can do all sort of stuff like:
time.strftime("%d//%m//%Y %H~%M~%S")
Hope that helped
Please add the required separators.
import time
timestr = time.strftime("%d/%m/%Y %H:%M:%S")
print(timestr)
from datetime import datetime
datetime_str = '20200607-080127'
datetime_object = datetime.strptime(datetime_str, '%Y%m%d-%H%M%S')
print(type(datetime_object))

Python convert date to datetime

I have following string of date "2018-05-08" and I would like to convert it into the datetime format of python like: "2018-05-08T00:00:00.0000000". I understand I can combine the string like:
> date = "2018-05-08"
> time = "T00:00:00.0000000"
> date+time
'2018-05-08T00:00:00.0000000'
But is there pythonic way of doing it where I can use libraries like datetime?
You can use datetime module like this example:
import datetime
date = "2018-05-08"
final = datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%Y-%m-%dT%H:%M:%S.%f')
print(final)
Output:
2018-05-08T00:00:00.000000
For more details visit datetime's documentation
Use this code
from datetime import date
from datetime import datetime
d = date.today()
datetime.combine(d, datetime.min.time())
from datetime import datetime
datetime.strftime(datetime.strptime('2018-05-08','%Y-%m-%d'),'%Y-%m-%dT%H:%M:%S.%f')
OUTPUT:
'2018-05-08T00:00:00.000000'
from datetime import datetime
date_as_datetime = datetime.strptime("2018-05-08", '%Y-%m-%d')
date_as_string = date_as_datetime.strftime('%Y-%m-%dT%H:%M:%S')
print(date_as_string)
Out:
'2018-05-08T00:00:00'
See strptime/strftime options here: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
Yes, you can use the datetime module and the strftime method from that module. Here is an example below if we want to format the current time.
import datetime
current_time = datetime.datetime.now()
formatted_time = datetime.datetime.strftime(current_time, "%Y-%h-%d %H:%m:%S")
print(formatted_time)
Here is the output:
2018-May-08 13:05:16

having issues with python time object

i am having a small program that gets time from system and dose some arithemetic on it.
here is my code:
import time
import datetime
c_time = time.strftime("%H:%M;%S")
# now to convert it into time object i am using
c_time_converted = datetime.datetime.strptime(c_time,"%H:%M;%S")
print c_time_converted
the output is of the form :
1900-01-01 11:39:40
but i want only the time part when i split and perform other operations on it i get a string returned but i need in time format so that i can perform time arethemetic
i want it in form 11: 39:40
Just use .time():
from datetime import datetime
c_time = '2015-06-08 23:13:57'
c_time_converted = datetime.strptime(c_time,"%Y-%m-%d %H:%M:%S")
print c_time_converted
print c_time_converted.time()
prints:
2015-06-08 23:13:57
23:13:57
EDIT (addressing a comment regarding datetime)
Here is a simple example of using a difference in datetime:
from datetime import timedelta
c_time_later = '2015-06-08 23:50:21'
c_time_later_converted = datetime.strptime(c_time_later,"%Y-%m-%d %H:%M:%S")
dt = c_time_later_converted - c_time_converted
print dt # time I was away from stackoverflow
prints: 0:36:24
now we will add dt time back to c_time above and see that we recover c_time_later:
print (c_time_converted + dt).time()
prints: 23:50:21
Or add an hour using timedelta:
print (c_time_converted + timedelta(hours=1)).time() # prints 00:13:57
I made a minor change in your code. Please check. it will give you time only.
import time
import datetime
c_time = time.strftime("%H:%M:%S")
# now to convert it into time object i am using
c_time_converted = datetime.datetime.strptime(c_time,"%H:%M:%S")
print c_time_converted.time()
Try to this.
import time
import datetime
c_time = time.strftime("%H:%M;%S")
c_time_converted = datetime.datetime.strptime(c_time,"%H:%M;%S")
print c_time_converted.time()
Output :
12:04:31
Get Current Date and Time.
from datetime import datetime
print datetime.now().strftime('%Y-%m-%d %H:%M:%S')
Output:
2015-06-09 12:09:09

How to convert a time to a string

I am using the date time lib to get the current date. After obtaining the current date I need to convert the obtained date to in to a string format. Any help is appriciated
from datetime import date
today=date.today()
print today
You can use today.strftime(format). format will be a string as described here http://docs.python.org/library/time.html#time.strftime . Example:
from datetime import date
today = date.today()
today.strftime("%x")
#>>> '01/31/11'
datetime.strftime allows you to format a datetime however you want
stringDate = str(today)
If that's what you want.
In Python3, you can use f-string formatting:
from datetime import date
today=date.today()
today_str = f"{today:%m/%d/%y}"
today_str
#>>>'06/24/22'

Categories

Resources