SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change date format for datetime

    Can someone tell me how to convert the following "datetime" mysql type which is currently showing up as Tue Jan 24 21:32:00 -0500 2006 to just January 24, 2006 in Ruby?
    John Saunders

  2. #2
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Man I have searched and searched Google on how to do this and I cannot find anything on how to do this? It seems like something that many people would want to know how to do? Is datetime not the best way to store a date if I'm just wanting to display it as "January 24, 2006"?
    John Saunders

  3. #3
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I did a little digging and this is what I came up with. I'm new to Ruby though, so I'm sure there is a one-liner out there that will do this nicely.

    Code:
    #set date
    date = Date.parse("Tue Jan 24 21:32:00 -0500 2006")
    
    date.month #outputs 1-12
    date.day #outputs 1-31
    date.year #outputs year..
    In terms of getting the month back to words... I'm not sure other than making an array of months..
    Code:
      months = %w(January February March ...)
      months[date.month] # might work?? not sure
    Hope that helps a little

  4. #4
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ignore my last post, I've got it now =)

    Use:
    Code:
    time = Time.parse("your date string")
    time.strftime("%B %d, %Y") #outputs January 24, 2006
    read more about the options in the Time Class Documentation

  5. #5
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for this stupid questions but I'm just on Chapter 6 of Agile Web Development with Rails and am trying to create my app as I go along. Do I put that code in app/helpers/schedule_helper.rb? If so, do I then call this in app/controllers/schedule_controller.rb and if so how?
    John Saunders

  6. #6
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like they just use it in the view..
    Code:
    product.date_available.strftime("formatting...")
    If you wanted to throw it into a method so you could just call it from whereever

    Code:
    def format_date(timestamp)
      Time.parse(timestamp).strftime("%B %d, %Y")
    end
    Then if you throw it in the helper you should be able to access it from the schedule_controller (and view?) by doing
    Code:
     formatted_date_string = format_date(myobject.date_column)
    That is the basic idea, anyways... I'm still having my own problems with rails, and haven't picked up the finess of a lot of the other ruby coders on these boards yet. Let me know how that works for you.

  7. #7
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm afraid you lost me. Here's what I have. It isn't working but can you let me know what I'm doing wrong?

    app/schedule/index.html
    Code:
    <h1>Schedule</h1>
    
    <% for schedule in @schedules %>
    <h2><%= schedule.date %></h2>
    <% end %>
    app/helpers/schedule_helper.rb
    Code:
    module ScheduleHelper
      formatted_date_string = format_date(myobject.date)
    end
    app/controllers/schedule_controller.rb
    Code:
    class ScheduleController < ApplicationController
      def index
        @schedules = Schedule.discussions
      end
      def format_date(date)
        Time.parse(timestamp).strftime("%B %d, %Y")
      end
    end
    app/models/schedule.rb
    Code:
    class Schedule < ActiveRecord::Base
      validates_presence_of :topic, :speaker, :position
    
      def self.discussions
          find (:all)
      end
    end
    John Saunders

  8. #8
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, a few things...

    In your Model..
    does self.discussion call everything out of the database??
    If so, it is not needed to have that method, and in your controller you can just do:
    Code:
    @schedules = Schedule.find(:all)
    Then, in your view you should be doing something like this:
    Code:
    <h1>Schedule</h1>
    
    <% for schedule in @schedules %>
    <h2><%= format_date(schedule.date) %></h2>
    <% end %>
    and move your format_date method from your controller to your helper and get rid of the other line you have in there.

    If you are getting rails errors, let me know what they are.

    edit:
    Alternatively, I think this might work (untested), you can give it a try..

    in your Model you can add
    Code:
      def formatted_date
        Time.parse(self.date).strftime("%B %d, %Y")
      end
    and then in your view do something like:

    Code:
      <h1>Schedule</h1>
    
    <% for schedule in @schedules %>
    <h2><%= schedule.formatted_date %></h2>
    <% end %>

  9. #9
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried the code in the bottom half of your post and got this:

    Code:
    NoMethodError in Schedule#index
    
    Showing app/views/schedule/index.rhtml where line #4 raised:
    
    private method `gsub!' called for Tue Jan 24 21:32:00 -0500 2006:Time
    Extracted source (around line #4):
    
    1: <h1>Schedule</h1>
    2: 
    3: <% for schedule in @schedules %>
    4: <h2><%= schedule.formatted_date %></h2>
    5: <div class="column1">
    6:   <strong>Topic:</strong><br />
    7:   <strong>Speaker:</strong><br />
    RAILS_ROOT: /Users/john/Sites/rails/test.com/public/../config/..
    
    Application Trace | Framework Trace | Full Trace
    /usr/local/lib/ruby/1.8/date/format.rb:255:in `_parse'
    /usr/local/lib/ruby/1.8/time.rb:240:in `parse'
    #{RAILS_ROOT}/app/models/schedule.rb:8:in `formatted_date'
    #{RAILS_ROOT}/app/views/schedule/index.rhtml:4:in `_run_rhtml_schedule_index'
    #{RAILS_ROOT}/app/views/schedule/index.rhtml:3:in `each'
    #{RAILS_ROOT}/app/views/schedule/index.rhtml:3:in `_run_rhtml_schedule_index'
    Request
    
    Parameters: None
    
    Show session dump
    
    Response
    
    Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}
    John Saunders

  10. #10
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can I see the full code for your Model?
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  11. #11
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean what's in "app/models/schedule.rb"? Here's what's currently in that file:

    Code:
    class Schedule < ActiveRecord::Base
      validates_presence_of :topic, :speaker, :position
    
      def self.discussions
          find (:all)
      end
      def formatted_date
          Time.parse(self.date).strftime("%B %d, %Y")
      end
    end
    John Saunders

  12. #12
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which type is self.date? It should be a string.

  13. #13
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm.. I'm not really sure whats going on now. Try
    Code:
    def formatted_date
          Time.parse(self.date.to_s).strftime("%B %d, %Y")
    end
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  14. #14
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was able to get it to work with just using the code below. Thanks for your help!

    Code:
    <%= schedule.date.strftime('%B %d, %Y') %>
    John Saunders

  15. #15
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that will work.
    I was attempting to put it as a method so that you could call it from anywhere in your app though, in case there are other views that you wanted to call it from. That way, if you ever decide that you want to change the way you display it, you just change one function, rather than tracking down all the places you wrote it in your views. It is part of the DRY (Don't Repeat Yourself) philosiphy.
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  16. #16
    SitePoint Zealot mpdesigns's Avatar
    Join Date
    Apr 2004
    Location
    Wisconsin
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keep it Symple!

  17. #17
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the link.

    Can you tell me which file I'd need to put this in? I need to use it for a frontend and backend view and have put it in model/schedule.rb but it comes up with "undefined local variable or method `schedule' for #<#<Class:0x266df94>:0x266df6c>" on both of the frontend and backend pages.

    Code:
      def formatted_date
        Time.parse(self.date.to_s).strftime("%B %d, %Y")
      end
    John Saunders


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •