C # Classic topics-year, week, date calculation

Source: Internet
Author: User
Tags iso 8601

//////Gets the start date and end date of the week specified in the year. The start date is Monday. /// Year (1 to 9999)///week (1 to 53)///For first week////When this method returns, contains the System.DateTime value for the start date of the week specified by the parameter year and weeks, or System if it fails. Datetime.minvalue. If the parameter year or weeks exceeds the valid range, the operation fails. The parameter is passed without initialization. When this method returns, contains the System.DateTime value for the end date of the week specified by the parameter year and weeks, or System.DateTime.MinValue if it fails. If the parameter year or weeks exceeds the valid range, the operation fails. The parameter is passed without initialization. public static bool Getdaysofweeks (int year, int weeks, CalendarWeekRule weekrule, out datetime first, out datetime las T) {//Initialize out parameter first = Datetime.minvalue; last = Datetime.minvalue;//Do not explain ... if (Year < 1 | year > 9999) retur n false; Up to 53 weeks a year the Earth people know ... if (Weeks < 1 | weeks >) return false; Take the first day of the year as a benchmark ... Why? Easy to chant ... DateTime Firstcurr = new DateTime (year, 1, 1); Remove the first day of the year for calculation ... DateTime firstnext = new DateTime (year + 1, 1, 1); Convert the day of the week to a number ... Sunday Special treatment ... ISO 8601 standard ... int dayofweekfirst = (int) Firstcurr.dayofweek; if (Dayofweekfirst = = 0) Dayofweekfirst = 7; Get the first day of the week without validation ... first = Firstcurr.adddays ((weeks-1) * 7-dayofweekfirst + 1); //The day of the week is the date of the previous year ... if (first. Year < year} {switch (weekrule) {case Calendarweekrule.firstday:////Don't explain ... first = Firstcurr; break, Case CALENDARW Eekrule.firstfullweek://Postpone one week ... AddDays (7); Break Case Calendarweekrule.firstfourdayweek://The first day of the week is less than 4 days before the week is postponed ... if (firstcurr.subtract). Days > 3) {first = first. AddDays (7); } break; Default:break; }}//Get unverified weekend days ... last = first. AddDays (7). AddSeconds (-1); Switch (weekrule) {case Calendarweekrule.firstday://Weekend Day is the date of next year ... if (last. Year > Year) last = Firstnext.addseconds (-1); else if (last. DayOfWeek! = dayofweek.monday) last = first. AddDays (7-(int) first. DayOfWeek). AddSeconds (-1); Break Case Calendarweekrule.firstfourdayweek://Weekends are less than 4 days before the first day of the next year, one week ahead ... if (last. Year > Year && firstnext.subtract (first). Days < 4) {first = first. AddDays (-7); Last = last. AddDays (-7); } break; Default:break; } return true; } ////// Gets the number of weeks of the specified date in a year //private int getweekofyear (DateTime DT) {GregorianCalendar GC = new GregorianCalendar (); return GC. Getweekofyear (DT, Calendarweekrule.firstday, dayofweek.monday); }

C # Classic topics-year, week, date calculation

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.