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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:04:44+00:00 2026-06-10T20:04:44+00:00

I have a method which takes a DateTime (currently as a string) which i

  • 0

I have a method which takes a DateTime (currently as a string) which i then convert to a DateTime object, for example:

def foo(time)
  the_time = Time.parse(time)
  ...
end

and so,

foo("2012-09-03")

I would like to also be able to parse a DateTime object:

foo(Time.now)

But Time.parse complains that I am not giving it a string:

NoMethodError: undefined method `gsub!' for 2012-08-08 15:59:00 UTC:Time

Is there an elegant (one line?) way to return a DateTime object from a string or DateTime object (i would prefer to avoid using if statements)

Thanks

Edit: Thanks for the comments – I have no real aversion to using an if statement, I was just seeing if there was anything nicer

  • 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-10T20:04:46+00:00Added an answer on June 10, 2026 at 8:04 pm

    Here’s one way to do it without if statements:

    def foo(time)
      the_time = Time.parse(time.to_s)
      # ...
    end
    

    However if you’re looking for an elegant solution, I’d doubt that re-parsing date can be considered as such. Why do you think if is a bad option? Here’s how I’d do it:

    def foo(time)
      the_time = time.respond_to?(:to_time) ? time.to_time : Time.parse(time.to_s)
      # ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method which takes a directory path as a string. In the
I have a Java method which takes a String as parameter and returns a
Say I have a method called GetCatsByColor which takes a color as a string,
I have a method which takes a list and do some processing on it
i have a method which takes in a DotNetOpenAuth (formally known as DotNetOpenId) Response
I have a method which takes an array of strings as parameter and queries
I have an ATL COM component method which takes a BSTR as an in
I have the following method, which takes in the name of a file as
I have this method which lets me translate the position of an object, animating
I have a method that (sometimes) takes in a string in the format dddd

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.