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

The Archive Base Latest Questions

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

I use chronic to return parsed date objects like so: last_pay_date = Date.parse(Chronic.parse(‘2011-11-04’).strftime(%Y-%m-%d)) two_weeks_ago

  • 0

I use chronic to return parsed date objects like so:

last_pay_date = Date.parse(Chronic.parse('2011-11-04').strftime("%Y-%m-%d"))
two_weeks_ago = Date.parse(Chronic.parse("two weeks ago").strftime("%Y-%m-%d"))

puts last_pay_date # 2011-11-04
puts last_pay_date.class # Date
puts two_weeks_ago # 2011-10-24
puts two_weeks_ago.class # Date

But what I really want to do is date a date object like last_day_date and have the date two weeks before returned, like so:

two_weeks_ago = Date.parse(Chronic.parse("two weeks before #{last_pay_date}").strftime("%Y-%m-%d"))

This returns nil, as other attempts, both with variables and hard coded:

last_pay_date = Date.parse(Chronic.parse('2011-11-04').strftime("%Y-%m-%d"))
two_weeks_ago = Chronic.parse("two weeks before #{last_pay_date}")
puts two_weeks_ago.class #NilClass
puts two_weeks_ago.nil? #true

two_weeks_ago = Chronic.parse("2 weeks before 2011-11-04 05:00:00")
puts two_weeks_ago.class #NilClass
puts two_weeks_ago.nil?  #true

Questions:

  1. Can I use chronic to parse offsets of existing date objects? (eg. 14 days before this date instead of just +14 days).

  2. Can I use chronic to parse offsets of existing dates at all?

  3. How is this best accomplished, even if the solution is outside of chronic?

Edit: I am running Ruby 1.9.2 without rails. These are strait up ruby scripts, though I’ll happily include any gems needed to do the job.

  • 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:03:23+00:00Added an answer on May 26, 2026 at 6:03 pm

    First off, is this Ruby 1.8 or 1.9? If 1.9, you have the Date#strptime method available, which makes parsing dates from strings pretty easy.

    Once you have your pay date, getting two weeks earlier from it is pretty easy, even without Chronic.

    Date.strptime("11/7/2011", "%m/%d/%Y").next_day(-14)

    If this is Ruby 1.8, then if ActiveSupport is available (ie, if this is a Rails app), there are Fixnum extensions that make this trivial.

    last_pay_date - 2.weeks

    However, I suspect that it is not available. In Ruby 1.8.7, Date#next_day is private, so you can’t invoke it directly, but you can still invoke it if you really want to.

    Date.parse("11/7/2011").send(:next_day, -14)

    If you don’t mind bypassing the private protection, this works just fine.

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

Sidebar

Related Questions

I'm trying to use Chronic inside my non-rails project. When I try to get
Use Html element SELECT in aspx page like: <SELECT id=MySelection name=MySelection runat=server DataValueField=ID DataTextField=Name></SELECT>
Use trap to capture signals like this: i=-1;while((++i<33)); do trap echo $i >> log.txt
Use new MediaPlayer by Yahoo. Content parse automatic - greate, but I use ajax
Does anyone know of a .NET date/time parser similar to Chronic for Ruby (handles
Use Case: End-User searches for something and an ArrayCollection is returned with Result objects.
Use Elixir and have two entities -- Voter and Candidate -- with many to
USE [Fk_Test2] GO /****** Object: Table [dbo].[Owners] Script Date: 08/20/2010 16:52:44 ******/ SET ANSI_NULLS
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Use case: A does something on his box and gots stuck. He asks B

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.