Filtering Dates by Periods like (Today, Yesterday,This Week, Last Week, This Month)
public class DatePeriod
{
private DateTime _StartDate=DateTime.Now ;
private DateTime _EndDate=DateTime.Now ;
public DatePeriod()
{
//
// TODO: Add constructor logic here
//
}
public enum DatePeriods
{
Today,
Yesterday,
ThisWeek,
LastWeek,
ThisMonth
}
public DateTime StartDate
{
get { return _StartDate; }
set { _StartDate = value; }
}
public DateTime EndDate
{
get { return _EndDate; }
set { _EndDate = value; }
}
public string GetPeriods(DatePeriods periods)
{
DateTime referenceDate;
DateTime startDate = new DateTime();
DateTime endDate = new DateTime();
string dateString = string.Empty;
switch (periods)
{
case DatePeriods.Today:
referenceDate = DateTime.Now;
dateString = referenceDate.ToString("dd-MMM-yyyy");
_StartDate = DateTime.Now;
break;
case DatePeriods.Yesterday:
referenceDate = DateTime.Now.AddDays(-1);
dateString = referenceDate.ToString("dd-MMM-yyyy");
_StartDate = referenceDate;
break;
case DatePeriods.ThisWeek:
referenceDate = DateTime.Now;
startDate = referenceDate;
endDate = referenceDate.AddDays(6);
dateString = startDate.ToString("dd-MMM-yyyy") + " - " + endDate.ToString("dd-MMM-yyyy");
_StartDate = startDate;
_EndDate = endDate;
break;
case DatePeriods.LastWeek:
referenceDate = DateTime.Now.AddDays(-7);
startDate = referenceDate;
endDate = startDate.AddDays(6);
dateString = startDate.ToString("dd-MMM-yyyy") + " - " + endDate.ToString("dd-MMM-yyyy");
_StartDate = startDate;
_EndDate = endDate;
break;
case DatePeriods.ThisMonth:
referenceDate = DateTime.Now;
startDate = new DateTime(referenceDate.Year, referenceDate.Month, 1);
endDate = startDate.AddMonths(1).AddDays(-1);
dateString = startDate.ToString("dd-MMM-yyyy") + " - " + endDate.ToString("dd-MMM-yyyy");
_StartDate = startDate;
_EndDate = endDate;
break;
}
return dateString;
}
}
Comments
Post a Comment