Solved

ruby on rails date formatting

Posted on 2014-12-28
1
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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