Ananas
Ananas

Reputation: 135

Create a datetime using strings in rails

What I would like to do is to go from "2013", "December", "20" and to create 2013-12-20.

Does someone have an idea ? Thanks !

Upvotes: 0

Views: 73

Answers (4)

shivam
shivam

Reputation: 16506

This is an extension to @Marc-Alexandre Bérubé 's answer to get your desired format:

"20 december 2013".to_date.strftime("%Y-%m-%d")
# => "2013-12-20"

Upvotes: 1

Djafar Hodi-Zoda
Djafar Hodi-Zoda

Reputation: 61

You can do this:

Date.new(2013, 12, 20)

You can read more about Date here

Upvotes: 1

Nikhil Khullar
Nikhil Khullar

Reputation: 733

For your required format you can use this bit for formatting:

strftime("The date is %y-%m-%d")

This can be called on any time object.

Upvotes: 1

Rails provide nice converters in part of it's framework

2.1.2 :001 > "20 december 2013".to_date
 => Fri, 20 Dec 2013

Upvotes: 2

Related Questions