Rails 3 time output
I'm now working on my output of the feeds I'm taking in from some site. What I'm currently doing is Time, and i want it to be displayed in a maybe, little be special way.. like this..
today, 14:12
yesterday, 15:34
27/12, 15:24
i have 开发者_JS百科this in my code
= news.entry_published.strftime("%d/%m, %H:%M")
That gives me an error saying
undefined method `strftime' for "2010-12-30 19:26:00.000000":String
And it dosn't do what i want with the days..
Edit:
- @date = DateTime.strptime(news.entry_published, "%Y-%m-%d %H:%M:%S")
= @date.strftime("%d/%m, %H:%M")
Now works, and gives this output
30/12, 19:26
But i still have to check if it is today, yesterday or just another day. Cheers, Oluf.
As error message suggests, datatype for entry_published is String. I believe problem is that in database entry_published is varchar datatype, right?
To solve this you have to do one of following things: - Change entry_published database datatype to datetime - First convert this string to Date and afterwards apply this strftime method.
Your issue now is that you want a date, but you actually have a datetime.
It should be easy enough to check if the date is today doing something like...
@date.to_date == Date.today
Yesterday...
@date.to_date == Date.yesterday
精彩评论