Hardcode generate todays date and in miltarytime 23:00 (python)

Viewed 29

I am currently writing a function that will generate as hardcode today's date and the time as 23:00. For example, if today is June 1, 2022 it will be 2022-06-01T23:00. If today's date is May 14, 2022 it will be 2022-05-14T23:00. I currently only can set it to the midnight of YYYY-MM-DD-00:00:00 even I change the numbers to 23:00.

Here is my implementation:

time = datetime.date(scheduled_time).strftime("%m %d %y")
replace_time = time.replace('23', '0') //here can be 23:00
format_date = datetime.strptime(replace, "%m %d %y")
return format_date

Any clue on how to print the 23:00 part

1 Answers

This will use today's date and the time 23:00 to create a datetime object:

from datetime import datetime
t = datetime.now().replace(hour=23, minute=0, second=0, microsecond=0)
print(t)
s = t.strftime("%Y-%m-%dT%H:%M")
print(s)

Output:

2022-06-18 23:00:00
2022-06-18T23:00
Related