Forum: Ruby on Rails Date Formats, assigning, viewing, validating, saving, changi

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Db7238007950074e9e73b76a81910406?d=identicon&s=25 tonypm (Guest)
on 2007-05-07 20:29
(Received via mailing list)
Hi,

I know Date formats are a common topic, but I've spent most of the day
googling and reading - and I cannot make things make sense.

I want to use European date format dd/mm/yy.
I am happy to stick with the ISO format yyyy-mm-dd
It is easy to display this in uk format using strftime(%d/%m/%Y)

Now all I need to do is simply fix the validation so that it switches
the uk format to the ISO format.
I was thinking of just using gsub to reverse the elements, but as I
tried some examples I discovered some things that I cant explain and
am hoping for some guidance.

 (s.order_date is a date attribute)
using the console
s=OrderDate.new

s.order_date="07-08-2008"
s.order_date.month=08  (seems to assume uk format)

s.order_date="07/08/2008"
s.order_date.month=07  (seems to assume us format)

I dont know why the two different formats are assumed.  I cannot
recall setting up anything to treat the nn-nn-nnnn format as uk
format.

now if in the console, I enter s.  The object with the textual date
form is displayed.

So I thought I could fix the problem so that the user can enter / or -
by substituting these before validation.  BUT

If I enter s.order_date="31/08/2007", I can still see the textual
display but in the console, s.order_date returns nil because the date
format is invalid.  I cant find a way to directly access the raw
object field before it gets turned into a Date class.  I need to get
at the raw attribute value.

Is this the right approach, or am I missing something simple?  I dont
really want to add plugins unless I understand what they are changing,
but it really seems simplest to me to use strftime to display and a
simple before_update method to fix the format.

Thanks for any help
tonypm
This topic is locked and can not be replied to.