• C# Net 计算周(可正推和逆推)


    C# Net 计算周(可正推和逆推)

    拷贝代码(方法):

            /// <summary>
            /// 计算周
            /// </summary>
            /// <param name="Time">一个时间点,用来计算周(必须有年月日)</param>
            /// <param name="WeekCount">推测的周数量</param>
            /// <param name="Add">是正推(加)还是负推(减)</param>
            /// <param name="ThisWeek">是否包含此时间点所在的周</param>
            /// <returns></returns>
            public static List<TimeWeek> GetTimeWeek(DateTime Time, int WeekCount, bool Add, bool ThisWeek)
            {
                List<TimeWeek> timeWeeks = new List<TimeWeek>();
                TimeWeek week = new TimeWeek();
                week.Week = ConvertWeekOfYear(Time);
                int dayOfTheWeek = (int)Time.DayOfWeek;
                int CountLength = dayOfTheWeek == 0 ? 7 : dayOfTheWeek;
                //如果包含当前周,就加入到返回列表.同时推算的周数减一
                if (ThisWeek)
                {
                    DateTime mon = Time.Date.AddDays(1 - CountLength);//本周一
                    DateTime sun = Time.Date.AddDays(7 - CountLength).AddDays(1).AddSeconds(-1);//本周日
                    week.BeginTime = mon;
                    week.EndTime = sun;
                    timeWeeks.Add(week);
                    WeekCount--;
                }
                if (Add)
                {
                    for (int i = 1; i <= WeekCount; i++)
                    {
                        DateTime mon = Time.Date.AddDays(1 - CountLength + i * 7);//往前推的周数计算星期一
                        DateTime sun = Time.Date.AddDays(7 - CountLength + i * 7).AddDays(1).AddSeconds(-1);
                        TimeWeek Downweek = new TimeWeek();
                        Downweek.Week = ConvertWeekOfYear(mon);
                        Downweek.BeginTime = mon;
                        Downweek.EndTime = sun;
                        timeWeeks.Add(Downweek);
                    }
                }
                else
                {
                    for (int i = 1; i <= WeekCount; i++)
                    {
                        DateTime mon = Time.Date.AddDays(1 - CountLength - i * 7);//往回推的周数计算星期一
                        DateTime sun = Time.Date.AddDays(7 - CountLength - i * 7).AddDays(1).AddSeconds(-1);
                        TimeWeek upweek = new TimeWeek();
                        upweek.Week = ConvertWeekOfYear(mon);
                        upweek.BeginTime = mon;
                        upweek.EndTime = sun;
                        timeWeeks.Add(upweek);
                    }
                }
                return timeWeeks.OrderBy(t => t.BeginTime).ToList();
            }
    
            /// <summary>
            /// 返回指定年份的周
            /// </summary>
            /// <param name="time"></param>
            /// <returns></returns>
            public static int ConvertWeekOfYear(DateTime time)
            {
                System.Globalization.GregorianCalendar gc = new System.Globalization.GregorianCalendar();
                return gc.GetWeekOfYear(time, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
            }

    拷贝代码(自定义类):

        /// <summary>
        /// 时间周
        /// </summary>
        public class TimeWeek
        {
            /// <summary>
            /// 第几周(从开年的1月1日计算)
            /// </summary>
            public int Week { get; set; }
            /// <summary>
            /// 这周的开始时间(一般是周一)
            /// </summary>
            public DateTime BeginTime { get; set; }
            /// <summary>
            /// 这周的结束时间(一般是周日)
            /// </summary>
            public DateTime EndTime { get; set; }
        }
  • 相关阅读:
    自学华为IoT物联网_10 IoT联接管理平台配置及开发实验1
    自学华为IoT物联网_09 OceanConnect业务流程
    自学华为IoT物联网_08 IoT连接管理平台介绍
    自学华为IoT物联网_07 物联网安全
    自学华为IoT物联网_06 智慧家庭物联网常见问题及解决方案
    自学华为IoT物联网_05 能源工业物联网常见问题及解决方案
    自学华为IoT物联网_04 车联网常见问题及解决方案
    自学华为IoT物联网_03 公共事业物联网常见问题及解决方案
    自学华为IoT物联网_02 常见物联网通信技术
    OpenDCIM-19.01操作手册
  • 原文地址:https://www.cnblogs.com/ping9719/p/11904500.html
Copyright © 2020-2023  润新知