I have two DateTime fields that I’d like to automatically fill so that the PeriodFrom field is set to 7 days ago and the PeriodTo field is set to today’s date.
At present I have them set up so that the PeriodFrom is set to the first day of the month with the following code:
PeriodFrom = DateTime.Now.FirstDayOfMonth();
PeriodTo = DateTime.Today;
where FirstDayOfMonth() is the extension method:
public static DateTime FirstDayOfMonth(this DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, 1);
}
Is there a quick extension method that I could write to set the date to exactly 1 week ago?
Usage, as you already understand, like this: