Based on the current time create a slot for 30 minutes time interval using Python

1k Views Asked by At

I have to create a two slot based on the current time assume that current time is 11:25 so based on the current time i have to create two slot for 30 minutes of time period which suppose to be 11:00 to 11:30 and another one is 11:30 to 12:00 and these slot should be dynamic so if the time is 12:20 then my new slot should be 12:00 to 12:30 and 12:30 to 1:00 only two slots using python.

I am newbie to python any help would be appreciated.

todayDate = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(todayDate)
currentTime = datetime.now()
now= 30

As I told i am newbie to python i have tried this much and stuck to logic.

1

There are 1 best solutions below

4
On

You can use the Pandas module. Specifically the date_range function.

import pandas

# Get current time
now = pandas.Timestamp.now()    

# Round current time down to the nearest 30 minutes.
now = now.floor('30min')

# create a range of 3 datetime objects with a frequency of 30 minutes.
datetimes = pandas.date_range(start=now, periods=3, freq='30min')

# Get only the Hour and Minute of those datetime objects.
times = [datetime.strftime("%H:%M") for datetime in datetimes]

# Use list slicing to repeat second time.
times = times[:2] + times[1:]

print(times)

returns

['12:00', '12:30', '12:30', '13:30']

When run any time between 12:00 and 12:29.