timedelta

Convert timedelta to floating-point

我怕爱的太早我们不能终老 提交于 2019-11-28 18:34:16
I got a timedelta object from the subtraction of two datetimes. I need this value as floating point for further calculations. All that I've found enables the calculation with floating-points, but the result is still a timedelta object. time_d = datetime_1 - datetime_2 time_d_float = float(time_d) does not work. You could use the total_seconds method: time_d_float = time_d.total_seconds() In recent versions of Python, you can divide two timedelta s to give a float. This is useful if you need the value to be in units other than seconds. time_d_min = time_d / datetime.timedelta(minutes=1) time_d

What is the difference between “datetime.timedelta” and “dateutil.relativedelta.relativedelta” when working only with days?

你。 提交于 2019-11-28 18:31:15
What is the difference between datetime.timedelta (from Python's standard library) and dateutil.relativedelta.relativedelta when working only with days? As far as I understand, timedelta only supports days (and weeks), while relativedelta adds support for periods defined in terms of years, months, weeks or days, as well as defining absolute values for year, month or day. (remember, for the purposes of this question, I don't have to worry about hours, minutes or seconds) Considering that I'm only working with datetime.date objects, and only interested in periods defined by the number of days,

How can I display timedelta in hours:min:sec?

无人久伴 提交于 2019-11-28 11:45:37
I am exporting a list of timedeltas to csv and the days really messes up the format. I tried this: while time_list[count] > datetime.timedelta(days = 1): time_list[count] = (time_list[count] - datetime.timedelta(days = 1)) + datetime.timedelta(hours = 24) But it's instantly converted back into days and creates an infinite loop. By default the str() conversion of a timedelta will always include the days portion. Internally, the value is always normalised as a number of days, seconds and microseconds, there is no point in trying to 'convert' days to hours because no separate hour component is

Python: Difference of 2 datetimes in months [duplicate]

房东的猫 提交于 2019-11-28 05:44:00
Possible Duplicate: Best way to find the months between two dates (in python) I would like to know how I can have the exact number of months for this difference: date1 = datetime.strptime(str('2011-08-15 12:00:00'), '%Y-%m-%d %H:%M:%S') date2 = datetime.strptime(str('2012-02-15'), '%Y-%m-%d') date2-date1 results in datetime.timedelta(183, 43200) I would like to know the exact number of months, in this case it should return 5 and not 6 (because of the hour) Using calendar module to find out how many days each month has, you can simply count the months. from calendar import monthrange from

Understanding timedelta

末鹿安然 提交于 2019-11-28 03:31:42
Given the python code below, please help me understand what is happening there. start_time = time.time() time.sleep(42) end_time = time.time() uptime = end_time - start_time human_uptime = str(datetime.timedelta(seconds=int(uptime))) So I get the difference between start time and end time , on line 5 I round up the duration by casting and what now, what's the further explanation? I know what delta means(average or difference), but why do I have to pass seconds = uptime to timedelta and why does the string casting works so nicely that I get HH:MM:SS ? utdemir Because timedelta is defined like:

Convert datetime.time into datetime.timedelta in Python 3.4

别等时光非礼了梦想. 提交于 2019-11-28 01:55:50
I am trying to convert two "durations", however I am currently receiving a TypeError due to one being a datetime.timedelta and one being a datetime.time : TypeError: unorderable types: datetime.time() <= datetime.timedelta() What is an efficient way to convert a datetime.time to a datetime.timedelta ? I have checked the docs and there is no built-in method for conversion between these two types. datetime.time() is not a duration , it is a point in a day. If you want to interpret it as a duration, then convert it to a duration since midnight: datetime.combine(date.min, timeobj) - datetime.min

Python format timedelta greater than 24 hours for display only containing hours?

狂风中的少年 提交于 2019-11-28 01:34:39
How do I format timedelta greater than 24 hours for display only containing hours in Python? >>> import datetime >>> td = datetime.timedelta(hours=36, minutes=10, seconds=10) >>> str(td) '1 day, 12:10:10' # my expected result is: '36:10:10' I acheive it by: import datetime td = datetime.timedelta(hours=36, minutes=10, seconds=10) seconds = td.total_seconds() hours = seconds // 3600 minutes = (seconds % 3600) // 60 seconds = seconds % 60 str = '{}:{}:{}'.format(int(hours), int(minutes), int(seconds)) >>> print(str) 36:10:10 Is there a better way? May be defining your class that inherits

Django: Using F arguments in datetime.timedelta inside a query

大憨熊 提交于 2019-11-28 00:03:53
Using Django model syntax, if I do this: ThatModel.objects.filter( last_datetime__lte=now + datetime.timedelta(seconds=F("interval"))) I get: TypeError: unsupported type for timedelta days component: ExpressionNode Is there a way to make this work with pure Django syntax (and not parsing all the results with Python)? ruddra From django docs: Django provides F expressions to allow such comparisons. Instances of F() act as a reference to a model field within a query. These references can then be used in query filters to compare the values of two different fields on the same model instance. That

Why is Python datetime time delta not found?

不羁的心 提交于 2019-11-27 23:39:51
问题 I am trying to make an array of dates in mmddyyyy format. The dates will start on the current day and then go two weeks into the future. So it all depends on the starting date. When I run my code I get an error that states: Traceback (most recent call last): File "timeTest.py", line 8, in <module> day = datetime.timedelta(days=i) AttributeError: type object 'datetime.datetime' has no attribute 'timedelta' I am not sure why this is happening because after searching online, I noticed that

How to get the sum of timedelta in Python?

霸气de小男生 提交于 2019-11-27 23:33:50
Python: How to get the sum of timedelta? Eg. I just got a lot of timedelta object, and now I want the sum. That's it! datetime combine method allows you to combine time with a delta datetime.combine(date.today(), time()) + timedelta(hours=2) timedelta can be combined using usual '+' operator >>> timedelta(hours=3) datetime.timedelta(0, 10800) >>> timedelta(hours=2) datetime.timedelta(0, 7200) >>> >>> timedelta(hours=3) + timedelta(hours=2) datetime.timedelta(0, 18000) >>> You can read the datetime module docs and a very good simple introduction at http://www.doughellmann.com/PyMOTW/datetime/