Asp.net (C #) provides a powerful time and date Processing instance,

Source: Internet
Author: User

Asp.net (C #) provides a powerful time and date Processing instance,

This example describes the powerful time and date processing class implemented by asp.net (C. We will share this with you for your reference. The details are 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 and date processing class // </summary> public class DateTimeHelper {# region returns the number of days of the current year. // <summary> returns the number of days of the current year </summary> /// <param name = "iYear"> year </param> /// <Returns> days of the current year </returns> public static int GetDaysOfYear (int iYear) {int cnt = 0; if (IsRuYear (iYear) {// one day after the leap year is: february is 29 days cnt = 366;} else {// -- one day less than a leap year: February is 28 days cnt = 365;} return cnt ;} # endregion # region returns the number of days of the current year. // <summary> Number of days of the current year </summary> // <param name = "dt"> date </param>/ // <returns> the number of days in the current year </returns> public static int GetDaysOfYear (DateTime idt) {int n; // gets the year of the input parameter to determine whether it is a leap year. N = idt. year; if (IsRuYear (n) {// one day after a leap Year: February is 29 days return 366;} else {// -- one day less than a leap Year: february is 28 days return 365 ;}} # endregion # region returns the number of days of the current month. // <summary> Number of days of the current month </summary> // <param name = "iYear"> year </param>/ // <param name = "Month"> Month </param> // <returns> days </returns> public static int GetDaysOfMonth (int iYear, int Month) {int days = 0; switch (Month) {case 1: days = 31; break; case 2: if (IsRuYea R (iYear) {// One day more than a leap year, that is, February is 29 days = 29;} else {// -- one day less than a leap year: february: 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 returns this month How many days are there? // <summary> How many days are there in this month </summary> /// <param name = "dt"> date </param> /// <returns> days </returns> public static int GetDaysOfMonth (DateTime dt) {// -------------------------------- // -- Obtain the current year and month information from dt -- // -------------------------- // int year, month, days = 0; year = dt. year; month = dt. month; // -- obtain the number of days of the current Month based on the year and Month information. Switch (month) {case 1: days = 31; break; case 2: if (IsRuYear (year) {// one day after a leap year is: february is 29 days = 29;} else {// -- one day less than a leap year: 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 1 2: days = 31; break;} return days ;} # endregion # region returns the name of the week of the current date /// <summary> returns the name of the week of the current date </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 returns the day of the week of the current date // <summary> returns the day of the week of the current date </summary> // <param name = "dt"> date </ param> // <returns> Number of the week </returns> public static string GetWeekNumberOfDay (DateTime idt) {string dt, week = ""; dt = idt. dayOfWeek. toString (); sw Itch (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 determines whether the year of the current date is a leap year, private function // <summary> determines whether the year of the current date is a leap year, private function </summary> /// <param name = "dt"> date </param> /// <ret Urns> is a leap year: True, not a leap year: False </returns> private static bool IsRuYear (DateTime idt) {// The format parameter is of the date type. // 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 determines whether the current year is a leap year, Private function // <summary> determines whether the current year is a leap year, private function </summary> /// <param name = "dt"> year </param> /// <returns> is a leap year: True, not a leap year: false </returns> private static bool IsRuYear (int iYear) {// format parameter: Year // For example: 2003 int n; n = iYear; if (n % 400 = 0) | (n % 4 = 0 & n % 100! = 0) {return true;} else {return false ;}# endregion # region converts the input string to a date. If the string format is invalid, return the current date /// <summary> /// to convert the input string to a date. If the string format is invalid, 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 converts a date object to a format string /// <summary> /// converts a date object to a format string /// </summary> /// <param name = "oDateTime"> date object </para M> /// <param name = "strFormat"> // format: /// "expiry date" === short date // "LONGDATE" = long date // other === custom format /// </param>/ // <returns> date string </returns> public static string ConvertDateToString (DateTime oDateTime, string strFormat) {string strDate = ""; try {switch (strFormat. toUpper () {case "Updated Date": 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 determines whether the date is valid. It must be later than January 1, 1800 /// <summary> /// determines whether the date is valid, the value must be greater than January 1, 1800 /// </summary> /// <param name = "strDate"> input date string </param> /// <returns> True/False </returns> public static bool IsDateTime (string strDate) {try {DateTime oDate = Da TeTime. parse (strDate); if (oDate. compareTo (DateTime. parse ("1800-1-1")> 0) return true; else return false;} catch (Exception) {return false ;}} # endregion # region returns the minute and second of the year, month, and day for the difference between the two dates. /// <summary> /// gets the difference between the two dates. /// </summary> /// <param name = "howtocompare"> the comparison method can be: year month day hour minute second </param> /// <param name = "startDate"> Start date </param> /// <param name = "endDate"> end date </param >/// <Returns> time difference </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 = Conve Rt. 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 working days between two dates /// <summary> // calculates the number of working days between two dates /// </summary> // <param name = "dtStart"> Start date </param> // /<Param name = "dtEnd"> end date </param> // <param name = "Flag"> whether to remove Saturday, sunday </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 }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.