根据年月日换算是第几周

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

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(2159)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示