How can I get 3 days back of current date and should not fall on weekends (Sat/Sun)
Let's say if date i select date as 03/28/2017. It should display date as 03/23/2017. It should not take sat/sun.
dto.ProcessEndDate.AddDays(-3);
How can I get 3 days back of current date and should not fall on weekends (Sat/Sun)
Let's say if date i select date as 03/28/2017. It should display date as 03/23/2017. It should not take sat/sun.
dto.ProcessEndDate.AddDays(-3);
This code assumes that if new date falls on a weekend, it will instead return the Friday before.
public static DateTime GetDateExcludeWeekends(DateTime date, int index)
{
var newDate = date.AddDays(-index);
if(newDate.DayOfWeek == DayOfWeek.Sunday)
{
return newDate.AddDays(-2);
}
if(newDate.DayOfWeek == DayOfWeek.Saturday)
{
return newDate.AddDays(-1);
}
return DateTime.Now;
}
You can tweak the logic, but the main thing is to look at the DayOfWeek
enum property of the DateTime
class.