Solved

ruby on rails date formatting

Posted on 2014-12-28
1
155 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
issues with gem updating and installing on new Ruby on Rails Production setup 5 767
Issue installing Ruby 1.9.2 on CentOS 5.8 6 1,296
Rails Clear Textarea 1 573
ruby image 1 112
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…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

932 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

10 Experts available now in Live!

Get 1:1 Help Now