zoukankan      html  css  js  c++  java
  • 根据年月日换算是第几周

    DateTimeExtensions

    http://stackoverflow.com/questions/2136487/calculate-week-of-month-in-net/2136549#2136549

    public static class DateTimeExtensions
        {
            private static readonly GregorianCalendar gregorianCalendar = new GregorianCalendar();
    
            public static int GetWeekOfMonth(this DateTime time)
            {
                DateTime first = new DateTime(time.Year, time.Month, 1);
                return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
            }
    
            public static int GetWeekOfYear(this DateTime time)
            {
                return gregorianCalendar.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
            }
        }

    GetWeekOfYear

    https://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear(v=vs.110).aspx

    Returns the week of the year that includes the date in the specified DateTime value.

    public virtual int GetWeekOfYear(
        DateTime time,
        CalendarWeekRule rule,
        DayOfWeek firstDayOfWeek
    )
    rule
    Type: System.Globalization.CalendarWeekRule

    An enumeration value that defines a calendar week.

    firstDayOfWeek
    Type: System.DayOfWeek

    An enumeration value that represents the first day of the week.

    CalendarWeekRule

    FirstDay   

    Indicates that the first week of the year starts on the first day of the year and ends before the following designated first day of the week. The value is 0.

    FirstFourDayWeek   

    Indicates that the first week of the year is the first week with four or more days before the designated first day of the week. The value is 2.

    FirstFullWeek  

    Indicates that the first week of the year begins on the first occurrence of the designated first day of the week on or after the first day of the year. The value is 1.

    根据年份,计算该年的最大周数

     public static class DateTimeExtensions
        {
            private static readonly GregorianCalendar gregorianCalendar = new GregorianCalendar();
    
            public static int GetWeekOfYear(this DateTime time)
            {
                return gregorianCalendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            }
    
            /// <summary>
            /// 根据年月日推算这一天属于哪一年的周
            /// 比如2017年01月01日,是2016年的第53周
            /// </summary>
            /// <param name="time"></param>
            /// <returns></returns>
            public static int GetYearOfWeek(this DateTime time)
            {
                int weeks = time.GetWeekOfYear();
                if (weeks == 53 && time.Month == 1)
                {
                    return time.Year - 1;
                }
                return time.Year;
            }
        }
     for (int year = 1989; year <= 2017; year++)
                    {
                        DateTime dateTime = new DateTime(year, 12, 31);
                        Console.WriteLine($"{dateTime.GetYearOfWeek()}年  最大周数={dateTime.GetWeekOfYear()}");
                    }

     2012年12月31日,是2012年的第53周,但是2011年没有第53周

    1989年 最大周数=52
    1990年 最大周数=53
    1991年 最大周数=53
    1992年 最大周数=53
    1993年 最大周数=52
    1994年 最大周数=52
    1995年 最大周数=52
    1996年 最大周数=53
    1997年 最大周数=53
    1998年 最大周数=53
    1999年 最大周数=52
    2000年 最大周数=52
    2001年 最大周数=53
    2002年 最大周数=53
    2003年 最大周数=53
    2004年 最大周数=53
    2005年 最大周数=52
    2006年 最大周数=52
    2007年 最大周数=53
    2008年 最大周数=53
    2009年 最大周数=53
    2010年 最大周数=52
    2011年 最大周数=52
    2012年 最大周数=53
    2013年 最大周数=53
    2014年 最大周数=53
    2015年 最大周数=53
    2016年 最大周数=52
    2017年 最大周数=52

  • 相关阅读:
    uva 10791 Minimum Sum LCM
    欧拉函数
    uva 10820 Send a Table
    本原勾股数组(PPT)
    uva 10003 Cutting Sticks
    生成随机数据
    uva 10759 Dice Throwing
    uva 106 Fermat vs. Pythagoras
    WPF 与 摄像头资料
    纠结于wpf 多国语言方案,希望各位指点
  • 原文地址:https://www.cnblogs.com/chucklu/p/6288906.html
Copyright © 2011-2022 走看看