using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace MSCL
{
/// summary>
/// 時(shí)間日期處理類
/// /summary>
public class DateTimeHelper
{
#region 返回本年有多少天
/// summary>返回本年有多少天/summary>
/// param name="iYear">年份/param>
/// returns>本年的天數(shù)/returns>
public static int GetDaysOfYear(int iYear)
{
int cnt = 0;
if (IsRuYear(iYear))
{
//閏年多 1 天 即:2 月為 29 天
cnt = 366;
}
else
{
//--非閏年少1天 即:2 月為 28 天
cnt = 365;
}
return cnt;
}
#endregion
#region 返回本年有多少天
/// summary>本年有多少天/summary>
/// param name="dt">日期/param>
/// returns>本天在當(dāng)年的天數(shù)/returns>
public static int GetDaysOfYear(DateTime idt)
{
int n;
//取得傳入?yún)?shù)的年份部分,用來判斷是否是閏年
n = idt.Year;
if (IsRuYear(n))
{
//閏年多 1 天 即:2 月為 29 天
return 366;
}
else
{
//--非閏年少1天 即:2 月為 28 天
return 365;
}
}
#endregion
#region 返回本月有多少天
/// summary>本月有多少天/summary>
/// param name="iYear">年/param>
/// param name="Month">月/param>
/// returns>天數(shù)/returns>
public static int GetDaysOfMonth(int iYear, int Month)
{
int days = 0;
switch (Month)
{
case 1:
days = 31;
break;
case 2:
if (IsRuYear(iYear))
{
//閏年多 1 天 即:2 月為 29 天
days = 29;
}
else
{
//--非閏年少1天 即:2 月為 28 天
days = 28;
}
break;
case 3:
days = 31;
break;
case 4:
days = 30;
break;
case 5:
days = 31;
break;
case 6:
days = 30;
break;
case 7:
days = 31;
break;
case 8:
days = 31;
break;
case 9:
days = 30;
break;
case 10:
days = 31;
break;
case 11:
days = 30;
break;
case 12:
days = 31;
break;
}
return days;
}
#endregion
#region 返回本月有多少天
/// summary>本月有多少天/summary>
/// param name="dt">日期/param>
/// returns>天數(shù)/returns>
public static int GetDaysOfMonth(DateTime dt)
{
//--------------------------------//
//--從dt中取得當(dāng)前的年,月信息 --//
//--------------------------------//
int year, month, days = 0;
year = dt.Year;
month = dt.Month;
//--利用年月信息,得到當(dāng)前月的天數(shù)信息。
switch (month)
{
case 1:
days = 31;
break;
case 2:
if (IsRuYear(year))
{
//閏年多 1 天 即:2 月為 29 天
days = 29;
}
else
{
//--非閏年少1天 即:2 月為 28 天
days = 28;
}
break;
case 3:
days = 31;
break;
case 4:
days = 30;
break;
case 5:
days = 31;
break;
case 6:
days = 30;
break;
case 7:
days = 31;
break;
case 8:
days = 31;
break;
case 9:
days = 30;
break;
case 10:
days = 31;
break;
case 11:
days = 30;
break;
case 12:
days = 31;
break;
}
return days;
}
#endregion
#region 返回當(dāng)前日期的星期名稱
/// summary>返回當(dāng)前日期的星期名稱/summary>
/// param name="dt">日期/param>
/// returns>星期名稱/returns>
public static string GetWeekNameOfDay(DateTime idt)
{
string dt, week = "";
dt = idt.DayOfWeek.ToString();
switch (dt)
{
case "Mondy":
week = "星期一";
break;
case "Tuesday":
week = "星期二";
break;
case "Wednesday":
week = "星期三";
break;
case "Thursday":
week = "星期四";
break;
case "Friday":
week = "星期五";
break;
case "Saturday":
week = "星期六";
break;
case "Sunday":
week = "星期日";
break;
}
return week;
}
#endregion
#region 返回當(dāng)前日期的星期編號
/// summary>返回當(dāng)前日期的星期編號/summary>
/// param name="dt">日期/param>
/// returns>星期數(shù)字編號/returns>
public static string GetWeekNumberOfDay(DateTime idt)
{
string dt, week = "";
dt = idt.DayOfWeek.ToString();
switch (dt)
{
case "Mondy":
week = "1";
break;
case "Tuesday":
week = "2";
break;
case "Wednesday":
week = "3";
break;
case "Thursday":
week = "4";
break;
case "Friday":
week = "5";
break;
case "Saturday":
week = "6";
break;
case "Sunday":
week = "7";
break;
}
return week;
}
#endregion
#region 判斷當(dāng)前日期所屬的年份是否是閏年,私有函數(shù)
/// summary>判斷當(dāng)前日期所屬的年份是否是閏年,私有函數(shù)/summary>
/// param name="dt">日期/param>
/// returns>是閏年:True ,不是閏年:False/returns>
private static bool IsRuYear(DateTime idt)
{
//形式參數(shù)為日期類型
//例如:2003-12-12
int n;
n = idt.Year;
if ((n % 400 == 0) || (n % 4 == 0 n % 100 != 0))
{
return true;
}
else
{
return false;
}
}
#endregion
#region 判斷當(dāng)前年份是否是閏年,私有函數(shù)
/// summary>判斷當(dāng)前年份是否是閏年,私有函數(shù)/summary>
/// param name="dt">年份/param>
/// returns>是閏年:True ,不是閏年:False/returns>
private static bool IsRuYear(int iYear)
{
//形式參數(shù)為年份
//例如:2003
int n;
n = iYear;
if ((n % 400 == 0) || (n % 4 == 0 n % 100 != 0))
{
return true;
}
else
{
return false;
}
}
#endregion
#region 將輸入的字符串轉(zhuǎn)化為日期。如果字符串的格式非法,則返回當(dāng)前日期
/// summary>
/// 將輸入的字符串轉(zhuǎn)化為日期。如果字符串的格式非法,則返回當(dāng)前日期。
/// /summary>
/// param name="strInput">輸入字符串/param>
/// returns>日期對象/returns>
public static DateTime ConvertStringToDate(string strInput)
{
DateTime oDateTime;
try
{
oDateTime = DateTime.Parse(strInput);
}
catch (Exception)
{
oDateTime = DateTime.Today;
}
return oDateTime;
}
#endregion
#region 將日期對象轉(zhuǎn)化為格式字符串
/// summary>
/// 將日期對象轉(zhuǎn)化為格式字符串
/// /summary>
/// param name="oDateTime">日期對象/param>
/// param name="strFormat">
/// 格式:
/// "SHORTDATE"===短日期
/// "LONGDATE"==長日期
/// 其它====自定義格式
/// /param>
/// returns>日期字符串/returns>
public static string ConvertDateToString(DateTime oDateTime, string strFormat)
{
string strDate = "";
try
{
switch (strFormat.ToUpper())
{
case "SHORTDATE":
strDate = oDateTime.ToShortDateString();
break;
case "LONGDATE":
strDate = oDateTime.ToLongDateString();
break;
default:
strDate = oDateTime.ToString(strFormat);
break;
}
}
catch (Exception)
{
strDate = oDateTime.ToShortDateString();
}
return strDate;
}
#endregion
#region 判斷是否為合法日期,必須大于1800年1月1日
/// summary>
/// 判斷是否為合法日期,必須大于1800年1月1日
/// /summary>
/// param name="strDate">輸入日期字符串/param>
/// returns>True/False/returns>
public static bool IsDateTime(string strDate)
{
try
{
DateTime oDate = DateTime.Parse(strDate);
if (oDate.CompareTo(DateTime.Parse("1800-1-1")) > 0)
return true;
else
return false;
}
catch (Exception)
{
return false;
}
}
#endregion
#region 獲取兩個(gè)日期之間的差值 可返回年 月 日 小時(shí) 分鐘 秒
/// summary>
/// 獲取兩個(gè)日期之間的差值
/// /summary>
/// param name="howtocompare">比較的方式可為:year month day hour minute second/param>
/// param name="startDate">開始日期/param>
/// param name="endDate">結(jié)束日期/param>
/// returns>時(shí)間差/returns>
public static double DateDiff(string howtocompare, DateTime startDate, DateTime endDate)
{
double diff = 0;
try
{
TimeSpan TS = new TimeSpan(endDate.Ticks - startDate.Ticks);
switch (howtocompare.ToLower())
{
case "year":
diff = Convert.ToDouble(TS.TotalDays / 365);
break;
case "month":
diff = Convert.ToDouble((TS.TotalDays / 365) * 12);
break;
case "day":
diff = Convert.ToDouble(TS.TotalDays);
break;
case "hour":
diff = Convert.ToDouble(TS.TotalHours);
break;
case "minute":
diff = Convert.ToDouble(TS.TotalMinutes);
break;
case "second":
diff = Convert.ToDouble(TS.TotalSeconds);
break;
}
}
catch (Exception)
{
diff = 0;
}
return diff;
}
#endregion
#region 計(jì)算兩個(gè)日期之間相差的工作日天數(shù)
/// summary>
/// 計(jì)算兩個(gè)日期之間相差的工作日天數(shù)
/// /summary>
/// param name="dtStart">開始日期/param>
/// param name="dtEnd">結(jié)束日期/param>
/// param name="Flag">是否除去周六,周日/param>
/// returns>Int/returns>
public static int CalculateWorkingDays(DateTime dtStart, DateTime dtEnd, bool Flag)
{
int count = 0;
for (DateTime dtTemp = dtStart; dtTemp dtEnd; dtTemp = dtTemp.AddDays(1))
{
if (Flag)
{
if (dtTemp.DayOfWeek != DayOfWeek.Saturday dtTemp.DayOfWeek != DayOfWeek.Sunday)
{
count++;
}
}
else
{
count++;
}
}
return count;
}
#endregion
}
}
更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net操作json技巧總結(jié)》、《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。