Home > Software design >  Number of 15 minutes intervals between two datetimes
Number of 15 minutes intervals between two datetimes

Time:03-28

from datetime import datetime, timedelta

now = datetime.now()
start = now   timedelta(minutes = 15)
finish = start   timedelta(minutes = 30)

How can I find the number of 15 minutes intervals exist between start and finish?

CodePudding user response:

from datetime import datetime, timedelta

now = datetime.now()
start = now   timedelta(minutes = 15)
finish = start   timedelta(minutes = 30)

elapsed = finish - start
number_of_intervals = elapsed / timedelta(minutes=15)

elapsed is the timedelta between start and finish. Divide by 15 minutes to calculate how many 15 minute intervals fit in there.

Note that this returns a float, so includes fractional intervals. Round as appropriate.

CodePudding user response:

You need to find the difference between start and finish in minutes, divide by 15, and make that an int:

now = datetime.now()
start = now   timedelta(minutes = 15)
finish = start   timedelta(minutes = 30)
difference = (finish - start).total_seconds()/60
quarters = int(difference/15)

CodePudding user response:

You can use time library instead of date time. time works with seconds and you should convert minutes to seconds:

import time
interval = 45*60
start = time.time()
finish = time.time()   interval
diff = finish - start
print(diff // (15*60))

CodePudding user response:

Simply compare start and finish like so:

from datetime import datetime, timedelta

now = datetime.now()
start = now   timedelta(minutes = 15)
finish = start   timedelta(minutes = 30)

elapsed = finish - start # This is a timedelta object

reference_interval = 15*60 # interval in seconds

number_of_intervals = elapsed.seconds/reference_interval

As pointed out by Sören, this will not work if 'elapsed' is more than one day, in which case, simply compute the number as follow:

number_of_intervals = (elapsed.days*86400 elapsed.seconds)/reference_interval
# (86400 seconds in a day)

CodePudding user response:

i would write something similar to this:

from datetime import datetime, timedelta

DATE_TIME_STRING_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'

from_date_time = datetime.strptime('2016-06-06T05:00:00.000Z',
                                   DATE_TIME_STRING_FORMAT)
to_date_time = datetime.strptime('2016-06-06T06:00:00.000Z',
                                 DATE_TIME_STRING_FORMAT)

date_times = [from_date_time.strftime(DATE_TIME_STRING_FORMAT)]
date_time = from_date_time
while date_time < to_date_time:
    date_time  = timedelta(minutes=15)
    date_times.append(date_time.strftime(DATE_TIME_STRING_FORMAT))

print(date_times)

Output:

['2016-06-06T05:00:00.000000Z', '2016-06-06T05:15:00.000000Z', '2016-06-06T05:30:00.000000Z', '2016-06-06T05:45:00.000000Z', '2016-06-06T06:00:00.000000Z']

Edit: If you are interested in just the number of 15 minute intervals you can use something like: from datetime import datetime, timedelta

DATE_TIME_STRING_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'

from_date_time = datetime.strptime('2016-06-06T05:00:00.000Z',
                                   DATE_TIME_STRING_FORMAT)
to_date_time = datetime.strptime('2016-06-06T06:00:00.000Z',
                                 DATE_TIME_STRING_FORMAT)

print((to_date_time-from_date_time) / timedelta(minutes=15))
  • Related