Solved

ruby on rails date formatting

Posted on 2014-12-28
1
168 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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