Forum: Ruby on Rails validating a date is a Monday

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.
bingo b. (Guest)
on 2009-04-10 10:26
I'm trying to do this...

  def validate

     errors.add_to_base "requested start date must be a monday" if
requested_start_date != Chronic.parse('monday')

  end


Not working though - this fails vaildation all the time (regardless of
whether the date is a monday or not)

What am I missing here ?


thanks

bb
Frederick C. (Guest)
on 2009-04-10 11:31
(Received via mailing list)
On Apr 10, 7:26 am, bingo bob <removed_email_address@domain.invalid>
wrote:
> whether the date is a monday or not)
>
Chronic.parse('monday') returns a particular monday (looks like next
monday), what else could it do. As a result your code doesn't check
that the day of week is monday, it checks whether it is a particular
monday at a particular time. You should look at the wday method on
Time/Date, which returns the day of week.

Fred
bingo b. (Guest)
on 2009-04-10 11:33

I have Chronic in place - can it help me work out if the date is a
Monday?
bingo b. (Guest)
on 2009-04-10 11:40
YOU GOT IT :-)... Thanks Fred (again)... easy, no need for chronic..

this seems to work


     errors.add_to_base "requested start date must be a monday" if
requested_start_date.wday != 1


1 being Monday

bb.
This topic is locked and can not be replied to.