C#-CODE//============================================================
// Producnt name: BoBoARTS.CodeMad
// Version: 1.0
// Author: 董广祥
// Auto generated at: 2009-8-7 13:36:20
//============================================================
using System;
/// <summary>
/// 日期帮助类
/// </summary>
public static class Utility
{
//当天
public static string NowTime()
{
return DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
}
//当天日期减去相应天数
public static string GetTimeByDayReduce(double day)
{
return DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.AddDays(day).Day.ToString();
}
//指定月份年份的第一天
public static DateTime GetFirstDayOfMonth(int Year, int Month)
{
return Convert.ToDateTime(Year.ToString() + "-" + Month.ToString() + "-1");
}
//指定月份年份的最后一天
public static DateTime GetLastDayOfMonth(int Year, int Month)
{
int Days = DateTime.DaysInMonth(Year, Month);
return Convert.ToDateTime(Year.ToString() + "-" + Month.ToString() + "-" + Days.ToString());
}
//当前一周时间
public static void ThisWeek(out string bDate, out string eDate)
{
DateTime firstDay = Convert.ToDateTime(NowTime());
double theday;
if (firstDay.DayOfWeek == DayOfWeek.Sunday) { theday = 7; }
else if (firstDay.DayOfWeek == DayOfWeek.Monday) { theday = 1; }
else if (firstDay.DayOfWeek == DayOfWeek.Tuesday) { theday = 2; }
else if (firstDay.DayOfWeek == DayOfWeek.Wednesday) { theday = 3; }
else if (firstDay.DayOfWeek == DayOfWeek.Thursday) { theday = 4; }
else if (firstDay.DayOfWeek == DayOfWeek.Friday) { theday = 5; }
else { theday = 6; }
double bday = -theday;
double eday = 7 - theday;
bDate = firstDay.AddDays(bday).ToString();
eDate = firstDay.AddDays(eday).ToString();
}
//过去一周时间
public static void BeforeWeek(out string bDate, out string eDate)
{
DateTime firstDay = Convert.ToDateTime(GetTimeByDayReduce(-ThisWeekLastDay() + 1));
double theday;
if (firstDay.DayOfWeek == DayOfWeek.Sunday) { theday = 7; }
else if (firstDay.DayOfWeek == DayOfWeek.Monday) { theday = 1; }
else if (firstDay.DayOfWeek == DayOfWeek.Tuesday) { theday = 2; }
else if (firstDay.DayOfWeek == DayOfWeek.Wednesday) { theday = 3; }
else if (firstDay.DayOfWeek == DayOfWeek.Thursday) { theday = 4; }
else if (firstDay.DayOfWeek == DayOfWeek.Friday) { theday = 5; }
else { theday = 6; }
double bday = -theday;
double eday = 7 - theday;
bDate = firstDay.AddDays(bday).ToString();
eDate = firstDay.AddDays(eday).ToString();
}
//本周最后一天
public static double ThisWeekLastDay()
{
DateTime firstDay = Convert.ToDateTime(NowTime());
double theday;
if (firstDay.DayOfWeek == DayOfWeek.Sunday) { theday = 7; }
else if (firstDay.DayOfWeek == DayOfWeek.Monday) { theday = 1; }
else if (firstDay.DayOfWeek == DayOfWeek.Tuesday) { theday = 2; }
else if (firstDay.DayOfWeek == DayOfWeek.Wednesday) { theday = 3; }
else if (firstDay.DayOfWeek == DayOfWeek.Thursday) { theday = 4; }
else if (firstDay.DayOfWeek == DayOfWeek.Friday) { theday = 5; }
else { theday = 6; }
return 7 - theday;
}
//本月第一天
public static DateTime GetFirstDayOfMonthTime()
{
return GetFirstDayOfMonth(DateTime.Now.Year, DateTime.Now.Month);
}
//本月最后一天
public static DateTime GetLastDayOfMonthTime()
{
return GetLastDayOfMonth(DateTime.Now.Year, DateTime.Now.Month);
}
//上月第一天
public static DateTime GetFirstDayOfBeforeMonthTime()
{
return GetFirstDayOfMonth(DateTime.Now.Year, DateTime.Now.Month - 1);
}
//上月最后一天
public static DateTime GetLastDayOfBeforeMonthTime()
{
return GetLastDayOfMonth(DateTime.Now.Year, DateTime.Now.Month - 1);
}
//根据下拉框状态获取相应的时间
public static void GetTime(out string lastDateTime, out string firstDateTime, string state)
{
switch (state)
{
case "": firstDateTime = ""; lastDateTime = "";
break;
case "today": firstDateTime = NowTime(); lastDateTime = "";
break;
case "yesterday": firstDateTime = NowTime(); lastDateTime = GetTimeByDayReduce(-1);
break;
case "last7days": firstDateTime = NowTime(); lastDateTime = GetTimeByDayReduce(-7);
break;
case "thisweek": ThisWeek(out firstDateTime, out lastDateTime);
break;
case "lastweek": BeforeWeek(out firstDateTime, out lastDateTime);
break;
case "thismonth": firstDateTime = GetFirstDayOfMonthTime().ToString(); lastDateTime = GetLastDayOfMonthTime().ToString();
break;
case "lastmonth": firstDateTime = GetFirstDayOfBeforeMonthTime().ToString(); lastDateTime = GetLastDayOfBeforeMonthTime().ToString();
break;
default: firstDateTime = ""; lastDateTime = "";
break;
}
}
}