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

  • Home
  • SEARCH
  • 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 8513849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:41:49+00:00 2026-06-11T04:41:49+00:00

My code: require ‘Date’ s = I’m going away on Oct 2, 2012th puts

  • 0

My code:

require 'Date'

s = "I'm going away on Oct 2, 2012th"
puts Date.parse(s)

=> 2012-10-02 

I want to delete the date from my strings, that I found with Date.parse(s). The problem is, I know that there is a date, but not how it was written in the string. I know that Date.parse found it and converted “2012-10-02” to a new format.

  • 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-11T04:41:50+00:00Added an answer on June 11, 2026 at 4:41 am

    Here is a quick and dirty solution. The function date_string
    returns just the portion of the string containing the date
    found by parse.

    require 'date'
    
    DATE_ERROR = -1
    
    # If the string doesn't contain a date, it raises an
    # exception.  This little helper routine catches the
    # exception.
    def get_date(s)
        date = 0
        begin
            date = Date.parse(s)
        rescue
            date = DATE_ERROR
        end
        date
    end
    
    # Returns just the part of the string containing the date
    def date_string(s)
        # First, find the date contained in the string
        date = get_date(s)
    
        return "" if date == DATE_ERROR
    
        # Repeatedly chop off characters from the front to find the
        # start of the date
        first = 1
        while date == get_date(s[first..-1])
            first += 1
        end
    
        # Repeatedly chop off characters from the end to find the
        # end of the date
        last = s.length - 2
        while date == get_date(s[0..last])
            last -= 1
        end
    
        #Return just the date
        s[first - 1..last + 1]
    end
    
    puts date_string("I'm going away on Oct 2, 2012th")
    puts date_string("I'm going away on 10/2/12 and not coming back")
    puts date_string("10 Nov 1999")
    puts date_string("I see no date here")
    

    This outputs:

    Oct 2, 2012
    10/2/12
    10 Nov 1999
    

    So you could do something like:

    s = "I'm going away on Oct 2, 2012th"
    datestr = date_string(s)
    s.gsub!(datestr, "")
    puts s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code require 'yaml' puts YAML.load( is_something: values: ['yes', 'no'] ).to_yaml produces --- is_something:
Here sample code: require 'nokogiri' require 'open-uri' begin doc = Nokogiri::HTML(open(url)) rescue puts Fehler
Code: require 'anemone' Anemone.crawl(http://www.example.com/) do |anemone| anemone.on_every_page do |page| puts page.url end end When
The Ruby code: require 'nokogiri' f = open(doc2.xml) # f.each { |line| puts line}
I have the following code: require 'rubygems' require 'net/http' require 'uri' url = URI.parse('http://servername.tld/up.txt')
Consider the following code: require 'nokogiri' # v1.5.2 doc = Nokogiri.XML('<body><a name=foo>ick</a></body>') puts doc.to_html
I want to show flash messages using sinatra-redirect-with-flash gem. Here's my ruby code: require
I have next code require 'rack/rpc' class Server < Rack::RPC::Server def hello_world Hello, world!
I have this code require 'mechanize' @agent = Mechanize.new page = @agent.get('http://something.com/?page=1') next_page =
I have the following code require 'test_helper' class ApplicationControllerTest < ActionController::TestCase test should display

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.