Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get time 17:00:00 today or yesterday?

If 17:00:00 today is already passed, then it should be today's date, otherwise - yesterday's. Today's time I get with:

test = datetime.datetime.now().replace(hour=17,minute=0,second=0,microsecond=0)

But I don't want to have future time. How can I fix it?

like image 304
LA_ Avatar asked Oct 02 '12 08:10

LA_


1 Answers

You could check if the current time is less than 17:00, if so, substract one day from the generated time object:

test = datetime.datetime.now().replace(hour=17,minute=0,second=0,microsecond=0)
if datetime.datetime.now() < test:
    test = test - datetime.timedelta(days=1)
like image 190
andrean Avatar answered Sep 21 '22 02:09

andrean