I am trying to create a event on the first and third monday of the month. Is there a way i can do this?

if the first and third monday are greater than today then create them for the next month. if the first monday is less than today then create for third monday and 1 monday of next month.

Any pointer would be of great help

    Date firstMonday = Date.today().toStartOfMonth().toStartOfWeek().addDays(1);    
    //add a week if "firstMonday" is in last month    
    if(firstMonday < date.today().toStartOfmonth()) firstMonday = firstMonday.addDays(7);
    Date thirdMonday = firstMonday.addDays(14);
| improve this answer | |
  • 2
    Almost. This won't work if used in January, because 12 > 1. Instead, consider if(firstMonday < date.today().toStartOfmonth())... which should be correct for all usual cases. – sfdcfox Dec 30 '15 at 0:21
  • nice catch sfdcfox, I edited the answer as you suggested – Jason Dec 30 '15 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.