The example in this article describes the asp.net (C #) Implementation of a powerful time-date processing class. Share to everyone for your reference, specific as follows:
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>///time-Date processing class///</summary> public class Datetimehelper {#region Returns how many days///<summary> returns this year how many days </summary>///<param name= "Iyear" > Year </param>///<re
Turns> this year </returns> public static int getdaysofyear (int iyear) {int cnt = 0;
if (Isruyear (iyear)) {//Leap year more 1 days namely: February for 29 days cnt = 366;
else {//--1 days less than leap year that is: February is 28 days cnt = 365;
} return CNT; #endregion #region How many days of the year///<summary> how many days are there </summary>///<param name= "DT" > Date </ Param>///<returns> days in the current days </returns> public static int Getdaysofyear (DateTimeIDT) {int n; Gets the year portion of the incoming parameter to determine if it is a leap years n = idt.
year;
if (Isruyear (n)) {//Leap Year 1 days more: February is 29 days return 366;
else {//--1 days less than leap year that is: February is 28 days return 365; } #endregion #region returns how many days///<summary> How many days this month </summary>///<param name= "Iyear" &G t; year </param>///<param name= "Month" > Month </param>///<returns> days </returns> public s
tatic int getdaysofmonth (int iyear, int Month) {int days = 0;
Switch (Month) {case 1:days = 31;
Break
Case 2:if (Isruyear (iyear)) {//Leap year more 1 days that is: February is 29 days = 29;
else {//--1 days less than leap year that is: February is 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 return this month how many days///<summary> How many days of this month </summary>///<param name= "DT" > Date </ param>///<returns> days </returns> public static int getdaysofmonth (DateTime dt) {//------ --------------------------////--from DT to get current year, month information--////--------------------------------/int years, Mont
H, days = 0; Year = dt.
year; month = dt.
Month;
--Use the month information to get the information of the days of the current months.
Switch (month) {case 1:days = 31;
BreakCase 2:if (isruyear) {//Leap year more than 1 days: February for 29 days = 29;
else {//--1 days less than leap year that is: February is 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 Return the day of the week name///<summary> return the current date of the week name </summary>///<param name= "DT" ; date </param>///<returns> week name </returns> publiC static String Getweeknameofday (DateTime idt) {string dt, week = ""; DT = IDT.
Dayofweek.tostring ();
switch (DT) {case "Mondy": Week = "Monday";
Break
Case "Tuesday": Week = "Tuesday";
Break
Case "Wednesday": Week = "Wednesday";
Break
Case "Thursday": Week = "Thursday";
Break
Case "Friday": Week = "Friday";
Break
Case "Saturday": Week = "Saturday";
Break
Case "Sunday": Week = "Sunday";
Break
Return week; #endregion #region Return the current date's week number///<summary> return the current date's week number </summary>///<param name= "DT"
; date </param>///<returns> week digit number </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 Determine whether the year to which the current date belongs is a leap years, private function///<summary> determine whether the current date is a leap year, private function </summary>///&L T;param name= "DT" > Date </param>///<returns> is a leap year: True, not leap year:false</returns> private static bool
Isruyear (DateTime IDT) {//formal parameter is date type//For example: 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 determine if the current year is a leap years, private functions///≪summary> determines whether the current year is a leap years, private functions </summary>///<param name= "DT" > Year </param>///<returns> is a leap age: True, not leap year:false</returns> private static bool Isruyear (int iyear) {//The form parameter is years//For example: 2003 I
NT N;
n = iyear; if ((n% 400 = 0) | |
(n% 4 = 0 && n% 100!= 0))
{return true;
else {return false; #endregion #region Converts the string entered into a date. If the format of the string is illegal, returns the current date///<summary>///converts the input string to a date.
If the format of the string is illegal, the current date is returned.
</summary>///<param name= "strinput" > Input string </param>///<returns> Date Object </returns>
public static datetime convertstringtodate (String strinput) {DateTime odatetime;
try {odatetime = DateTime.Parse (strinput);
catch (Exception) {odatetime = Datetime.today;
return odatetime; #endregion #region Convert a Date object to a format string///<summary>///Converts a Date object to a format string///</summary>///<param name= "Odatetime" > Date Object </param> <param name= "Strformat" >///format:///"shortdate" = = Short Date///"longdate" = = Long Date///other = self-determined Semantic format///</param>///<returns> Date string </returns> public static string convertdatetostring (Datet
IME odatetime, String strformat) {string strdate = ""; try {switch (Strformat.toupper ()) {case "shortdate": strdate = Odatetime.tos
Hortdatestring ();
Break
Case "Longdate": strdate = odatetime.tolongdatestring ();
Break
Default:strdate = odatetime.tostring (Strformat);
Break
} catch (Exception) {strdate = Odatetime.toshortdatestring ();
return strdate;
#endregion #region Judge If it is a legal date, it must be greater than January 1, 1800///<summary>To determine if it is a valid date, it must be greater than January 1, 1800///</summary>///<param name= "Strdate" > enter a date string </param>/// ;returns>true/false</returns> public static bool Isdatetime (string strdate) {try {D
Atetime odate = DateTime.Parse (strdate);
if (Odate.compareto (DateTime.Parse ("1800-1-1")) > 0) return true;
else return false;
catch (Exception) {return false; #endregion #region Get the difference between two dates to return the///<summary>///get the difference between two dates///</s ummary>///<param name= howtocompare > Comparison can be: Year month day hour minute second</param>/// Ram Name= "StartDate" > Start date </param>///<param name= "EndDate" > End date </param>///<returns>
Inter </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 calculates the number of weekday days between two dates///<summary>///calculates the number of weekday days between two dates///</summary> <param name= "Dtstart" > Start date </param>///<param Name= "Dtend" > End date </param>///<param name= "Flag" > excluding Saturday, Sunday </param>///<returns>int< ;/returns> public static int calculateworkingdays (datetime dtstart, datetime dtend, bool Flag) {int Coun
t = 0;
for (DateTime dttemp = Dtstart; dttemp < dtend; dttemp = dttemp.adddays (1)) {if (Flag) { if (Dttemp.dayofweek!= dayofweek.saturday && dttemp.dayofweek!= dayofweek.sunday) {cou
nt++;
} else {count++;
} return count;
} #endregion}}
More interested readers of asp.net related content can view the site topics: "asp.net operation JSON tips summary", "asp.net string operation tips Summary", "ASP.net Operation XML Skills summary", "asp.net file Operation skills Summary", " asp.net Ajax Skills Summary topic and the "ASP.net cache operation skills Summary."
I hope this article will help you to ASP.net program design.