Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8609763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:57:35+00:00 2026-06-12T03:57:35+00:00

The code below should return last Friday, 16:00:00. But it returns Friday of previous

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:57:36+00:00Added an answer on June 12, 2026 at 3:57 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I suppose button should has Close title in the code below, but it has
The code below takes an array value, if it's key exist it should echo
Please see my pseudo-code below. The code comments should explain my problem. I'm new
The below code seems like it should work; however, the blob in the database
The code below simply didn't work. document.getElementById('files').addEventListener('change', handleFileSelect, false); reported by firebug that this
I have a particular scenario below. The code below should print 'say()' function of
Can someone please post the simplest of working code that can get the last
Because of asking my last question , I've changed my code to use return
Below is an implementation of a function that returns the lexographically next permutation. This
Before you start reading... You should know that there are many questions below... I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.