Solved

ruby on rails date formatting

Posted on 2014-12-28
1
173 Views
Last Modified: 2014-12-28
I have three strings for a dates year, month, and day. I need to create a date object and format it like this:

Jan, 17 2015

I can't get this to work.  I substituted the variables that the strings represent.  The "20" is hard coded
        expiration_date_year = "20" + "15"
        expiration_date_month = "01"
        expiration_date_day = "17"

        expiration_date = Date.new(expiration_date_year, expiration_date_month, expiration_date_day)

Open in new window

0
Comment
Question by:jackjohnson44
1 Comment
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40520977
To format the date have a look at the strftime method. Also when creating a date object ion this way the inputs should be numerics not strings

expiration_date_year = "20" + "15"
expiration_date_month = "01"
expiration_date_day = "17"
date = Date.new(expiration_date_year.to_i, expiration_date_month.to_i, expiration_date_day.to_i)
formatted_date = date.strftime("%b, %m %Y")

Open in new window


http://www.ruby-doc.org/core-2.0/Time.html#method-i-strftime
http://ruby-doc.org/stdlib-2.2.0/libdoc/date/rdoc/Date.html
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question