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 6795937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:24:24+00:00 2026-05-26T18:24:24+00:00

Working with a Rails 3 application. Given a DateTime, I need to produce the

  • 0

Working with a Rails 3 application. Given a DateTime, I need to produce the Datetime for the previous Sunday. If the DateTime is a Sunday, then I want that Sunday itself back. Here’s some examples:

November 7, 2011 => November 6, 2011
November 6, 2011 => November 6, 2011
November 5, 2011 => October 30, 2011

Any ideas? I’m a bit stuck.

  • 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-05-26T18:24:25+00:00Added an answer on May 26, 2026 at 6:24 pm
    class DateTime
      def previous_sunday
        self - wday
      end
    end
    

    …but this is so easy, there’s no reason to monkey patch it into the class 🙂

    irb(main):001:0> require 'date'; now = DateTime.now
    #=> #<DateTime: 2011-11-10T21:10:04-07:00 (…)>
    irb(main):002:0> sunday = now - now.wday
    #=> #<DateTime: 2011-11-06T21:10:04-07:00 (…)>
    

    The wday (weekday) will be 0 for any Sunday, and so no change to the date will take place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a rails application that will migrate user content. I need to
I am working on a Rails application that needs to handle dates and times
I am thinking of working on a Rails application that uses PostgreSQL. I have
I'm trying to get tags working in my rails application and want to use
Background Currently, I am working on a Rails application. I have different products that
I am working on a rails application that uses big javascript libraries (e.g. jquery
I'm working on a Ruby on Rails application that used the Devise authentication system.
I have a working rails 3.1 application that uses the mercury wysiwyg editor, however
I'm working on a Rails application that tracks customer details (stored as a Customer
I'm working on a Rails application with some budget features... What i want to

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.