Questions tagged [timedelta]

Timedelta refers to the difference between two timestamps, which is a measure of elapsed time. It may also specifically refer to the 'timedelta' type in Python.

Timedelta refers to the difference between two timestamps, which is a measure of elapsed time. It may also specifically refer to the timedelta type in Python.

In it is represented by Time.deltaTime which holds the elapsed time between frames and allows to coordinate animations and movement of gameObjects.

It is similar to the TimeSpan structure in .NET.

Other languages may have similar features as well.

See also the and tags.

1357 questions
1147
votes
8 answers

How to subtract a day from a date?

I have a Python datetime.datetime object. What is the best way to subtract one day?
defrex
  • 15,735
  • 7
  • 34
  • 45
630
votes
18 answers

Adding days to a date in Python

I have a date "10/10/11(m-d-y)" and I want to add 5 days to it using a Python script. Please consider a general solution that works on the month ends also. I am using following code: import re from datetime import datetime StartDate =…
MuraliKrishna
  • 6,577
  • 3
  • 18
  • 11
498
votes
46 answers

Calculating the difference between two Java date instances

I'm using Java's java.util.Date class in Scala and want to compare a Date object and the current time. I know I can calculate the delta by using getTime(): (new java.util.Date()).getTime() - oldDate.getTime() However, this just leaves me with a…
pr1001
  • 21,727
  • 17
  • 79
  • 125
431
votes
34 answers

Format timedelta to string

I'm having trouble formatting a datetime.timedelta object. Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to…
mawcs
  • 4,357
  • 2
  • 18
  • 7
366
votes
14 answers

Convert a timedelta to days, hours and minutes

I've got a timedelta. I want the days, hours and minutes from that - either as a tuple or a dictionary... I'm not fussed. I must have done this a dozen times in a dozen languages over the years but Python usually has a simple answer to everything so…
Oli
  • 235,628
  • 64
  • 220
  • 299
324
votes
5 answers

Convert timedelta to total seconds

I have a time difference import time import datetime time1 = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())) ... time2 = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())) diff = time2 - time1 Now, how do I find the total…
ripper234
  • 222,824
  • 274
  • 634
  • 905
244
votes
4 answers

How to add hours to current time in python

I am able to get the current time as below: from datetime import datetime str(datetime.now())[11:19] Result '19:43:20' Now, I am trying to add 9 hours to the above time, how can I add hours to current time in Python?
Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
197
votes
6 answers

Python: Convert timedelta to int in a dataframe

I would like to create a column in a pandas data frame that is an integer representation of the number of days in a timedelta column. Is it possible to use 'datetime.days' or do I need to do something more manual? timedelta column 7 days,…
Asaf Hanish
  • 1,989
  • 2
  • 11
  • 6
185
votes
21 answers

Convert timedelta to years?

I need to check if some number of years have been since some date. Currently I've got timedelta from datetime module and I don't know how to convert it to years.
Migol
  • 8,161
  • 8
  • 47
  • 69
173
votes
11 answers

subtract two times in python

I have two datetime.time values, exit and enter and I want to do something like: duration = exit - enter However, I get this error: TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time How do I do this correctly? One…
Chaggster
  • 2,441
  • 3
  • 21
  • 14
161
votes
4 answers

Calculate Time Difference Between Two Pandas Columns in Hours and Minutes

I have two columns, fromdate and todate, in a dataframe. import pandas as pd data = {'todate': [pd.Timestamp('2014-01-24 13:03:12.050000'), pd.Timestamp('2014-01-27 11:57:18.240000'), pd.Timestamp('2014-01-23 10:07:47.660000')], 'fromdate':…
sbalajis
  • 1,939
  • 3
  • 14
  • 14
157
votes
12 answers

How to construct a timedelta object from a simple string

I'm writing a function that needs to parse string to a timedelta. The user must enter something like "32m" or "2h32m", or even "4:13" or "5hr34m56s"... Is there a library or something that has this sort of thing already implemented?
priestc
  • 33,060
  • 24
  • 83
  • 117
143
votes
5 answers

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

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…
Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111
102
votes
7 answers

Python: get datetime for '3 years ago today'

In Python, how do I get a datetime object for '3 years ago today'? UPDATE: FWIW, I don't care hugely about accuracy... i.e. it's Feb 29th today, I don't care whether I'm given Feb 28th or March 1st in my answer. Concision is more important than…
AP257
  • 89,519
  • 86
  • 202
  • 261
93
votes
2 answers

Understanding timedelta

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…
Paul
  • 20,883
  • 7
  • 57
  • 74
1
2 3
90 91