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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:38:41+00:00 2026-06-09T13:38:41+00:00

If I have a string such as string = 08/01/2012 How can I return

  • 0

If I have a string such as string = "08/01/2012"

How can I return “2012-08-01” in Ruby in one line?

e.g. take the set of digits after the last “/”, insert them at the beginning, then replace the “/”s with “-“s

  • 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-09T13:38:43+00:00Added an answer on June 9, 2026 at 1:38 pm

    Since you are dealing with dates, I’d go through the Date library (you have to require 'date' for some of the features) because it’s the most flexible and stable approach:

    Date.strptime(string, '%m/%d/%Y').strftime('%Y-%m-%d')
    #=> "2012-08-01"
    

    As you can see both strptime (“string parse time”) and strftime (“string format time”) take format strings that exactly describe what they are doing.

    If you don’t want to go through Date, you can use gsub like this:

    string.gsub(%r{(\d+)/(\d+)/(\d+)}, '\3-\1-\2')
    #=> "2012-08-01"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string such as big bad dog, how can I get an
Lets say a have a string such as this one: string txt = Lore
hey I have a string as such: 357902||190||RUE RACHELLE||ST|||LES CÈDRES|J7T1J9|QC I wish to take
If I have a string such as the following: String myString = SET(someRandomName, \hi\,
I have a string such as 'xxox-x' that I want to mask each line
If I have a 5 bit binary string such as '01010' , how can
If I have a string such as lotsofcrapsomethingimportantmoreotherstuff is it possible to get Regex
I have an xml string such as this: <s:Envelope xmlns:s=http://schemas.xmlsoap.org/soap/envelope/> <s:Body> <ExecuteResponse xmlns=http://schemas.microsoft.com/xrm/2011/Contracts/Services> <ExecuteResult
I have an input string such as x=y|a=b|c=10 etc which gets converted to dynamic
I have a bunch of records with dates formatted as a string such as

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.