using System;
namespace Moxell.Library
{
/// <summary>
/// DateTime 的摘要说明。
/// </summary>
public class DateTimeLibrary
{
public DateTimeLibrary()
{
}
// 返回指定日期是一年中的第几周
public static int GetWeek(DateTime dt)
{
int Week = 0;
System.Globalization.CultureInfo norwCulture = System.Globalization.CultureInfo.CreateSpecificCulture("no");
System.Globalization.Calendar cal = norwCulture.Calendar;
Week = cal.GetWeekOfYear(dt, System.Globalization.CalendarWeekRule.FirstFourDayWeek, System.DayOfWeek.Sunday);
return Week;
}
// 当月的第一天,接受日期参数
public static DateTime GetTheMonthFirstDay(DateTime theDate)
{
return new DateTime(theDate.Year,theDate.Month,1);
}
// 当月的第一天,接受字符串参数
public static DateTime GetTheMonthFirstDay(string YearMonth)
{
DateTime firstDay = new DateTime(Convert.ToInt32(YearMonth.Substring(0,4)),Convert.ToInt32(YearMonth.Substring(4,2)),1);
return firstDay;
}
//当月的最后一天日期
public static DateTime GetTheMonthLastDay(DateTime theDate)
{
return new DateTime(theDate.Year,theDate.Month,DateTime.DaysInMonth(theDate.Year,theDate.Month));
}
//返回一个指定日期年,月和今天是本月第几天的日期值
public static DateTime GetTheMonthToDay(DateTime theDate)
{
return new DateTime(theDate.Year,theDate.Month,DateTime.Today.Day);
}
//根据需要更的用法,可以自己理解一下
public static DateTime GetFromDate(DateTime theDate)
{
DateTime yesterday = theDate.AddDays(-1);
return GetTheMonthFirstDay(yesterday);
}
//根据需要更的用法,可以自己理解一下
public static DateTime GetToDate(DateTime theDate)
{
DateTime yesterday = theDate.AddDays(-1);
return yesterday;
}
//返回下月的第一天,可要仔细看了
public static DateTime GetTheCurrDDlSelectDate(string CurrDDlDate)
{
string CurrDateMonth= CurrDDlDate.Substring(4,2);
string CurrDateYear=CurrDDlDate.Substring(0,4);
int ConvMonth=Convert.ToInt32(CurrDateMonth);
int NeedConvMonth=ConvMonth+1;
string NeedRebackMonth=Convert.ToString(NeedConvMonth);
if(NeedRebackMonth.Length==1)
NeedRebackMonth="0"+NeedRebackMonth+"-01";
else
NeedRebackMonth=NeedRebackMonth+"-01";
string CurrNeedDate=CurrDateYear+"-"+NeedRebackMonth;
return Convert.ToDateTime(CurrNeedDate);
}
//下面这几个自己理解哈
public static DateTime GetDateByString(string stringDate)
{
DateTime ToDay = DateTime.Today;
DateTime resultDate = DateTime.MinValue;
switch (stringDate.ToLower())
{
case "today":
resultDate = ToDay;
break;
case "oneweek"://最近一周
resultDate = ToDay.AddDays(-7);
break;
case "onemonth"://最近一月
resultDate = ToDay.AddMonths(-1);
break;
case "threemonth"://最近三月
resultDate = ToDay.AddMonths(-2);
break;
case "sixmonth"://最近六月
resultDate = ToDay.AddMonths(-3);
break;
case "halfyear"://最近半年
resultDate = ToDay.AddMonths(-6);
break;
case "oneyear"://最近一年
resultDate = ToDay.AddYears(-1);
break;
case "twoyear"://最近二年
resultDate = ToDay.AddYears(-1);
break;
case "threeyear"://最近三年
resultDate = ToDay.AddYears(-1);
break;
default:
resultDate = ToDay.AddYears(-100);
break;
}
return resultDate;
}
public static DateTime GetBeginDateByString(string stringDate)
{
DateTime BeginDate = DateTime.MinValue;
if (stringDate.ToLower()=="all")
{
string Today="2006-01-01";
BeginDate =Convert.ToDateTime(Today);
}
else
{
BeginDate = new DateTime(Convert.ToInt32(stringDate.Substring(0,4)),Convert.ToInt32(stringDate.Substring(4,2)),1);
}
return BeginDate;
}
public static DateTime GetEndDateByString(string stringDate)
{
DateTime EndDate = DateTime.MinValue;
if (stringDate.ToLower()=="all")
{
DateTime today= DateTime.Today;
EndDate=GetTheMonthLastDay(today);
}
else
{
EndDate = new DateTime(Convert.ToInt32(stringDate.Substring(0,4)),Convert.ToInt32(stringDate.Substring(4,2)),1);
EndDate = GetTheMonthLastDay(EndDate);
}
return EndDate;
}
}
}