Topic: Remove date with cwday?


Re: Remove date with cwday?

cwday just returns an integer for the day of the week starting on Monday. So if the Date object is today (wednesday) cwday will return 3. You can use this to determine if a given day falls on a weekend:

if > 5
  # Date is on a weekend

However, getting this to work with a date selector will be difficult because the day selector is just numbers ranging from 1 to 31. The weekends depend upon the current month and year selected. If you want the weekends not selectable you'll need to use JavaScript to remake the "day" select box when changing the year or month so the weekends can't be selected.

A much easier solution is to do this on the server side (which you should implement anyway in case JavaScript isn't enabled), so if someone submits a date which falls on the weekend then you can return an error. You can do this with validation in your model (say you have a date attribute called "deadline")

def validate
  errors.add(:deadline, "can't be on a weekend") if deadline.cwday > 5

Hope that helps.

Last edited by ryanb (2006-08-16 14:03:39)

Railscasts - Free Ruby on Rails Screencasts

Re: Remove date with cwday?

Thanks, I think the validation method is my best bet.  I appriciate the help.