Last Monday Date

This function calculates the date of the last Monday based on the passed date.
/*********************************************************************************
  * Function signature:
  * Description:
  *                  
  *
  * Parameters
  *
  *
  *
  * Return Value
  *
  ********************************************************************************/
public DateTime FindLastMonday(DateTime date)
{
    //Cast day of the week to integer
    int dateOffset = (int)date.DayOfWeek;

    //DayOfWeek is enumeration starting from Sunday = 0; convert it to start from Monday    
    dateOffset--;
    if (dateOffset == -1) dateOffset = 6;
    
    //substract offset from the date passed as parameter
    return date.Subtract(new TimeSpan(dateOffset, 0, 0, 0));
}

Comments