Python set datetime hour to be a specific time

First, change datetime.date.today() to datetime.datetime.today() so that you can manipulate the time of the day.

Then call replace before turning the time into a string.

So instead of:

PERIOD=yesterday.strftime ('%Y-%m-%d') 
new_period=PERIOD.replace(hour=23, minute=30)

Do this:

new_period=yesterday.replace(hour=23, minute=30).strftime('%Y-%m-%d')
print new_period

Also keep in mind that the string you’re converting it to displays no information about the hour or minute. If you’re interested in that, add %H for hour and %M for the minute information to your format string.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)