C # in week, month, week, week start end time De method summary

Source: Internet
Author: User

1. C # Gets the current time is the week of the year, the week of the month

    private  static int getweeknuminmonth (DateTime daytime)              {                  int dayinmonth = daytime. Day;                  The first day of the month                  DateTime firstday = daytime. AddDays (1-daytime. Day);                  The first day of the month is the week of several                  int weekday = (int) Firstday.dayofweek = = 0? 7: (int) Firstday.dayofweek;                  A few days in the first week of this month                  int firstweekendday = 7-(weekday-1);                  The difference between the current date and the first week                  int diffday = dayinmonth-firstweekendday;                  Diffday = diffday > 0? diffday:1;                  Now is the first week, if divisible by 7 is minus one day                  int weeknuminmonth = ((diffday% 7) = = 0                   ? (DIFFDAY/7-1)                   : (DIFFDAY/7)) + 1 + (Dayinmonth > Firstweekendday? 1:0);                  return weeknuminmonth;              }  

2. Get the week of the year:

    GregorianCalendar gc = new GregorianCalendar ();                 int weekofyear = GC. Getweekofyear (DateTime.Now, Calendarweekrule.firstday, dayofweek.monday);                 Console.WriteLine (weekofyear);                 Console.ReadLine ();  

3, C # Get a few weeks this month and the weeks of the start and end time sample code

 <summary>///received a few weeks this month///</summary>//<param name= "a" ></p                 aram>//<returns></returns> public void NumWeeks (DateTime dt) { years int year = dt.                 year; monthly int month = dt.                 Month;                 The first day of the current month datetime weekstart = new DateTime (year, month, 1); The last day of the month DateTime monend = weekstart.addmonths (1).                 AddDays (-1);                 int i = 1;                 The first day of the current month is the days of the week int dayOfWeek = Convert.ToInt32 (weekStart.DayOfWeek.ToString ("D")); The first week end date of the month datetime weekEnd = DayOfWeek = = 0?                 WeekStart:weekStart.AddDays (7-dayofweek); Richtextbox2.text + = "+ + i +" Week start Date: "+ weekstart.toshortdatestring () +" End Date: "+ weekend.toshortdatestring () +" \ n                 "; When the date is less than or equal to the last day of the month WHile (Weekend.adddays (1) <= monend) {i++;                     The start time of the week Weekstart = weekend.adddays (1); End time of the week weekEnd = weekend.adddays (7) > Monend?                     MonEnd:weekEnd.AddDays (7); Richtextbox2.text + = "+ + i +" Week start Date: "+ weekstart.toshortdatestring () +" End Date: "+ weekend.toshortdatestring () +" \ n                 ";             } Richtextbox2.text + = year + "years" + month + "Monthly Total" + i + "week \ n"; }

4, My97datepicker date plug-in common function description

Http://jingyan.baidu.com/article/e6c8503c7244bae54f1a18c7.html

The above methods are actual use, pro-Test!!!

Summary of recommended

The source of each method is omitted.

C # in week, month, week, week start end time De method summary

Related Article

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.