asp.Net(C#)常用時間日付処理クラス
16635 ワード
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>
///
/// </summary>
public class DateTimeHelper
{
#region
/// <summary> </summary>
/// <param name="iYear"> </param>
/// <returns> </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> </returns>
public static int GetDaysOfYear(DateTime idt)
{
int n;
// ,
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> </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> </returns>
public static int GetDaysOfMonth(DateTime dt)
{
//--------------------------------//
//-- dt , --//
//--------------------------------//
int year, month, days = 0;
year = dt.Year;
month = dt.Month;
//-- , 。
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
/// <summary> </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
/// <summary> </summary>
/// <param name="dt"> </param>
/// <returns> </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 ,
/// <summary> , </summary>
/// <param name="dt"> </param>
/// <returns> :True , :False</returns>
private static bool IsRuYear(DateTime idt)
{
//
// :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 ,
/// <summary> , </summary>
/// <param name="dt"> </param>
/// <returns> :True , :False</returns>
private static bool IsRuYear(int iYear)
{
//
// :2003
int n;
n = iYear;
if ((n % 400 == 0) || (n % 4 == 0 && n % 100 != 0))
{
return true;
}
else
{
return false;
}
}
#endregion
#region 。 ,
/// <summary>
/// 。 , 。
/// </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
/// <summary>
///
/// </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
/// <summary>
///
/// </summary>
/// <param name="howtocompare"> :year month day hour minute second</param>
/// <param name="startDate"> </param>
/// <param name="endDate"> </param>
/// <returns> </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
/// <summary>
///
/// </summary>
/// <param name="dtStart"> </param>
/// <param name="dtEnd"> </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
}
}