20

The code below should return last Friday, 16:00:00. But it returns Friday of previous week. How to fix that?

now = datetime.datetime.now() test = (now - datetime.timedelta(days=now.weekday()) + timedelta(days=4, weeks=-1)) test = test.replace(hour=16,minute=0,second=0,microsecond=0) 

Upd. I use the following approach now - is it the best one?

now = datetime.datetime.now() if datetime.datetime.now().weekday() > 4: test = (now - datetime.timedelta(days=now.weekday()) + timedelta(days=4)) else: test = (now - datetime.timedelta(days=now.weekday()) + timedelta(days=4, weeks=-1)) test = test.replace(hour=16,minute=0,second=0,microsecond=0) 

Upd2. Just to give an example. Let's assume that today is Oct 5, 2012. In case current time is equal to or less than 16:00 it should return Sep 28, 2012, otherwise - Oct 5, 2012.

3
  • 4
    Holy crab nuggets. Split your code up! Commented Oct 2, 2012 at 8:40
  • @LA_ How do you want it to work if the current day is Friday? Commented Oct 2, 2012 at 8:52
  • @bohney, depends on the time. If 16:00 already passed, then it should be today. Otherwise - Friday of prev week. Commented Oct 2, 2012 at 9:03

6 Answers 6

47

The dateutil library is great for things like this:

>>> from datetime import datetime >>> from dateutil.relativedelta import relativedelta, FR >>> datetime.now() + relativedelta(weekday=FR(-1)) datetime.datetime(2012, 9, 28, 9, 42, 48, 156867) 
Sign up to request clarification or add additional context in comments.

6 Comments

This solution is good. You only need to add the .replace call for the time of day.
@LA_ That's a shame - wasn't aware that GAE was a constraint
I think I can install it separately. But would prefer to do it without 3rd party libraries. Anyway, thanks for your help.
How can I get last Friday by this code if the current day is Friday?
@Americancurl you can do min(mon-1day-relativedelta(weekday=MO(-1)), mon-relativedelta(weekday=MO(-1))
|
17

As in the linked question, you need to use datetime.date objects instead of datetime.datetime. To get a datetime.datetime in the end, you can use datetime.datetime.combine():

import datetime current_time = datetime.datetime.now() # get friday, one week ago, at 16 o'clock last_friday = (current_time.date() - datetime.timedelta(days=current_time.weekday()) + datetime.timedelta(days=4, weeks=-1)) last_friday_at_16 = datetime.datetime.combine(last_friday, datetime.time(16)) # if today is also friday, and after 16 o'clock, change to the current date one_week = datetime.timedelta(weeks=1) if current_time - last_friday_at_16 >= one_week: last_friday_at_16 += one_week 

2 Comments

Looks like your code if today is Oct 6, 2012 will return Sep 28, when Oct 5 is expected.
Thanks. I have to clearly explain what I need next time... Your code on Oct 5, 2012 will always return Sep 28, 2012. But in case current time is equal to or less than 16:00 it should return Sep 28, 2012, otherwise - Oct 5, 2012.
6

Simplest solution without dependency:

from datetime import datetime, timedelta def get_last_friday(): now = datetime.now() closest_friday = now + timedelta(days=(4 - now.weekday())) return (closest_friday if closest_friday < now else closest_friday - timedelta(days=7)) 

Comments

4

This was borrowed from Jon Clements, but is the full solution:

>>> from datetime import datetime >>> from dateutil.relativedelta import relativedelta, FR >>> lastFriday = datetime.now() + relativedelta(weekday=FR(-1)) >>> lastFriday.replace(hour=16,minute=0,second=0,microsecond=0) datetime.datetime(2012, 9, 28, 16, 0, 0, 0) 

Comments

1

The principle is the same as in your other question.

Get the friday of the current week and, if we are later, subtract one week.

import datetime from datetime import timedelta now = datetime.datetime.now() today = now.replace(hour=16,minute=0,second=0,microsecond=0) sow = (today - datetime.timedelta(days=now.weekday())) this_friday = sow + timedelta(days=4) if now > this_friday: test = this_friday else: test = this_friday + timedelta(weeks=-1) 

1 Comment

in this answer, I suppose that from Fr. 16:00:01 on, you want the datetime of this week.
0

Could be lame but to me simplest. Get the last day of the current month and start checking in a loop (which wouldn't cost anything since max loops before finding last friday is 7) for friday. if last day is not friday decrement and the check the day before.

import calendar from datetime import datetime, date def main(): year = datetime.today().year month = datetime.today().month x = calendar.monthrange(year,month) lastday = x[1] while True: z = calendar.weekday(year, month, lastday) if z != 4: lastday -= 1 else: print(date(year,month,lastday)) break if __name__ == "__main__": main() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.