Solved

ruby on rails date formatting

Posted on 2014-12-28
1
151 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now