Reputation: 147
Here is my code to get date and time when starting time is 10 am,( (2017, 10, 29, 10, 0) and then date time until 10 am on the next day. Now, I want to extract only the time from that and get 10:00 am to 10 am on the following day . So my result should be
(10:00,11:00,12:00,13:00,14:00,15:00,16:00,17:00,18:00,19:00,20:00,21:00,22:00,23:00,00:00,1:00,2:00,3:00,4:00,5:00,6:00,7:00,8:00,9:00,10:00)
I would be thankful if someone could assist me in this regard.
import datetime
a=datetime.datetime.combine(datetime.date.today(), datetime.time(10))
x = [a + datetime.timedelta(hours=i) for i in range(24)]
Upvotes: 0
Views: 456
Reputation: 476
try this
today = datetime.datetime.today()
x = [ ( (today + datetime.timedelta(hours=i)).hour ) for i in range(24)]
print(x)
each today plus i hour and get their attribute hour
Upvotes: 0
Reputation: 53525
You can modify it a bit:
["{}:00".format((a + datetime.timedelta(hours=i)).hour) for i in range(24)]
^
take only the hour
from the datetime object and format it the way you want.
OUTPUT
['10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '0:00', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00', '8:00', '9:00']
Upvotes: 0