I have a type object datetime.time. How to convert it to int, representing its duration in seconds? Or a string that I can then convert to a second representation by splitting?
datetime.time
You need to convert the object to datetime.timein datetime.timedeltaorder to use total_seconds().
datetime.timedelta
total_seconds()
It will return float, not the int asked in the question, but you can easily drop it.
float
>>> from datetime import datetime, date, time, timedelta >>> timeobj = time(12, 45) >>> t = datetime.combine(date.min, timeobj) - datetime.min >>> isinstance(t, timedelta) # True >>> t.total_seconds() 45900.0
Links I am inspired:
You can calculate it yourself:
from datetime import datetime t = datetime.now().time() seconds = (t.hour * 60 + t.minute) * 60 + t.second
, datetime.timedelta datetime.time.
datetime.time .
datetime.timedelta total_seconds(), , .
@Owl Max , timedelta. , timedelta.
, :
import datetime t = datetime.time(10, 0, 5) int(datetime.timedelta(hours=t.hour, minutes=t.minute, seconds=t.second).total_seconds())
(ps. )
Source: https://habr.com/ru/post/1680402/More articles:How to access an unknown form in the controller? - javascriptBind ng-options in ng model on boot (for example, bi-directional binding) - javascriptDifferences between getRootNav () and navCtrl () methods - angularDelphi TOpenDialog / TSaveDialog last used path - windowsIs there any way to look into a message with C # code in log4net? - c #Может ли std:: shared_ptr служить эффективной реализацией неизменяемых строк с подсчетом ссылок? - c++how ssh to google cluster container cluster nodes? - google-container-enginePassing JSON to JS using Django rendering - jsonLaravel few for many - phpHow to display drop-down list view below specific view in Android? - javaAll Articles