c# get start-date and last-date based on current date
I am doing an activ开发者_运维百科ity monitor based on date which is similar to stackoverflow Today,YesterDay,this week,Last week,this month,last Month
..... Based on current date how to get start-date
and end-date
for all these Today,YesterDay,this week,Last week,this month,last Month
in c#?
DateTime baseDate = DateTime.Today;
var today = baseDate;
var yesterday = baseDate.AddDays(-1);
var thisWeekStart = baseDate.AddDays(-(int)baseDate.DayOfWeek);
var thisWeekEnd = thisWeekStart.AddDays(7).AddSeconds(-1);
var lastWeekStart = thisWeekStart.AddDays(-7);
var lastWeekEnd = thisWeekStart.AddSeconds(-1);
var thisMonthStart = baseDate.AddDays(1 - baseDate.Day);
var thisMonthEnd = thisMonthStart.AddMonths(1).AddSeconds(-1);
var lastMonthStart = thisMonthStart.AddMonths(-1);
var lastMonthEnd = thisMonthStart.AddSeconds(-1);
public enum FrequencyType
{
None = 0,
Daily = 1,
Weekly = 2,
Monthly = 3,
Quarterly = 4,
Annually = 5,
}
private string[] GetRange(FrequencyType frequency, DateTime dateToCheck)
{
string[] result = new string [2];
DateTime dateRangeBegin = dateToCheck;
TimeSpan duration = new TimeSpan(0, 0, 0, 0); //One day
DateTime dateRangeEnd = DateTime.Today.Add(duration);
switch (frequency)
{
case FrequencyType.Daily:
dateRangeBegin = dateToCheck;
dateRangeEnd = dateRangeBegin;
break;
case FrequencyType.Weekly:
dateRangeBegin = dateToCheck.AddDays(-(int)dateToCheck.DayOfWeek);
dateRangeEnd = dateToCheck.AddDays(6 - (int)dateToCheck.DayOfWeek);
break;
case FrequencyType.Monthly:
duration = new TimeSpan(DateTime.DaysInMonth ( dateToCheck.Year, dateToCheck.Month) - 1 , 0, 0, 0);
dateRangeBegin = dateToCheck.AddDays((-1) * dateToCheck.Day + 1);
dateRangeEnd = dateRangeBegin.Add(duration);
break;
case FrequencyType.Quarterly:
int currentQuater = (dateToCheck.Date.Month - 1) / 3 + 1;
int daysInLastMonthOfQuarter = DateTime.DaysInMonth(dateToCheck.Year, 3 * currentQuater );
dateRangeBegin = new DateTime ( dateToCheck.Year, 3 * currentQuater - 2, 1);
dateRangeEnd = new DateTime(dateToCheck.Year, 3 * currentQuater , daysInLastMonthOfQuarter);
break;
case FrequencyType.Annually:
dateRangeBegin = new DateTime(dateToCheck.Year, 1, 1);
dateRangeEnd = new DateTime(dateToCheck.Year, 12, 31);
break;
}
result[0] = dateRangeBegin.Date.ToString();
result[1] = dateRangeEnd.Date.ToString();
return result;
}
http://zamirsblog.blogspot.com/2010/02/find-first-and-last-date-of-given-week.html
I hope this will help:
var begindate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("MM/dd/yyyy");
Console.WriteLine("FIRST DAY OF THE CURRENT MONTH (begindate): " + begindate);
var enddate=DateTime.Now.ToString("MM/dd/yyyy");
Console.WriteLine("TODAY'S DATE (enddate): " + enddate);
var comparebegindate = new DateTime(DateTime.Now.Year, DateTime.Now.Month-1, 1).ToString("MM/dd/yyyy");
Console.WriteLine("FIRST DAY OF THE LAST MONTH (comparebegindate): " + comparebegindate);
var comparedate = new DateTime(DateTime.Now.Year, DateTime.Now.Month - 1, DateTime.Now.Day).ToString("MM/dd/yyyy");
Console.WriteLine("TODAY'S DATE WITH LAST MONTH (comparedate): " + comparedate);
var compareenddate = new DateTime(DateTime.Now.Year, DateTime.Now.Month - 1, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)).ToString("MM/dd/yyyy");
Console.WriteLine("LAST MONTH'S LAST DATE (compareenddate): " + compareenddate);
精彩评论