1

I am trying to format my date from my RoR project, though having real issue understanding how this works. In php this is a simple date("format",time())

if i call just the standard time stored within the database i am getting 2012-08-12 22:25:00 +1000

I am trying to call the date formats event_time and event_date

Time::DATE_FORMATS[:month_and_year] = "%B %Y"
Time::DATE_FORMATS[:event_time] = "%l:%M %p"
Time::DATE_FORMATS[:event_date] = "%a, %e %b, %Y"

I am currently trying to call this like

= schedule.time.to_s(:event_time)

The issue I am having is that I am getting the following error:

wrong number of arguments(1 for 0)

Boss Nass
  • 3,384
  • 9
  • 48
  • 90
  • to_s wont accept any parameter so that error is there, have a look at [this](http://stackoverflow.com/questions/3654974/where-to-put-time-format-rules-in-rails-3), [this](http://api.rubyonrails.org/classes/Time.html) and [this](http://rails-bestpractices.com/posts/42-use-i18n-localize-for-date-time-formating) – Amol Pujari Aug 13 '12 at 05:33
  • have already read those pages, and have place my formats within `# config/initializers/time_formats.rb` this was how they have always been defined, though it doesnt appear to work – Boss Nass Aug 13 '12 at 05:38
  • is your schedule.time a date/time object i think it may be string – Pritesh Jain Aug 13 '12 at 05:39
  • its timestamp `time | timestamp without time zone | ` – Boss Nass Aug 13 '12 at 05:40
  • what is output of schedule.time.to_s – Pritesh Jain Aug 13 '12 at 06:08
  • output of schedule.time.to_s is `2012-08-12 22:25:00 +1000` – Boss Nass Aug 13 '12 at 06:10
  • everything seems to be fine here , one silly question which i encountered while testing is there .. before `to_s` like `schedule.time..to_s(:event_time)` – Pritesh Jain Aug 13 '12 at 06:37

3 Answers3

4

You can use strftime or esle your approach is also good if you need data frequently in this date format

The code you added works great for me

pritesh@cloudy-pritesh:~/railsdemo$ rails c
Loading development environment (Rails 3.2.6)
1.9.3p194 :001 > Date.today.to_s(:event_time)
 => "2012-08-13" 
1.9.3p194 :002 > Time::DATE_FORMATS[:month_and_year] = "%B %Y"
 => "%B %Y" 
1.9.3p194 :003 > Time::DATE_FORMATS[:event_time] = "%l:%M %p"
 => "%l:%M %p" 
1.9.3p194 :004 > Time::DATE_FORMATS[:event_date] = "%a, %e %b, %Y"
 => "%a, %e %b, %Y" 
1.9.3p194 :005 > Time.now.to_s(:event_time)
 => "11:13 AM" 

you can also do this by using Time.now.strftime("%B %Y")

My last guess, may be you are looping the records and shomewhere in the records there is a record with time having nil value

Pritesh Jain
  • 9,106
  • 4
  • 37
  • 51
  • problem is that it isnt working for me, or if i use schedule.time.strftime("%B %Y") im getting a nil class, these formats worked when we had the data stored within the db as a string, since changing to timestamp so we can could do time comparisons these formats have stopped – Boss Nass Aug 13 '12 at 05:50
  • the above worked for me with psql too may be i think your data format might have broken while column type change – Pritesh Jain Aug 13 '12 at 06:11
  • @Paul'Whippet'McGuane, My last may be you are looping the records and shomewhere in the records there is a record with time nil value – Pritesh Jain Aug 13 '12 at 06:49
  • totally correct there was a nil within the loop, ill need to ensure there is some validation so my users cannot add without a time being entered – Boss Nass Aug 13 '12 at 06:58
  • The Time.now.strftime("%B %Y") really helped me. Thanks! – mack Jun 08 '16 at 16:12
2

Use strftime

Time.now.strftime("%B %Y")   #August 2012

Then use something like following

Time.now.strftime(Time::DATE_FORMATS[:month_and_year])
Salil
  • 46,566
  • 21
  • 122
  • 156
  • im using strftime within my date formats file, i need to be able to call those formats around what is stored in the database – Boss Nass Aug 13 '12 at 05:34
  • i need to reference back to the timestamp store in the database, this doesnt seem to be a correct method – Boss Nass Aug 13 '12 at 05:40
0

Its simple,

schedule.time.strftime(Time::DATE_FORMATS[:format_type])   #August 2012

refer http://apidock.com/ruby/Time/strftime for detailed use.

PradeepKumar
  • 1,259
  • 9
  • 5