PCB 生产周期计算逻辑与代码实现

 

PCB生产周期计算逻辑:

代码实现:

调用代码: 

getWeek(DateTime.Now.Date, "WWYY", 0);

周期计算逻辑:

     /// <summary>
        /// 获取周期
        /// </summary>
        /// <param name="CurrentDate">填写计算日期</param>
        /// <param name="Format">YYYY,YY,WW,MM,DD</param>
        /// <param name="AddDay">增加周期日期</param>
        /// <returns></returns>
        public string getWeek(DateTime CurrentDate, string Format, int AddDay = 0)
        {
            string YYYY, YY, MM, DD, WW;
            CurrentDate = CurrentDate.AddDays(AddDay);
            //CurrentDate = DateTime.Parse("2014-12-29");
            YYYY = CurrentDate.Year.ToString();
            YY = YYYY.Substring(2, 2);
            MM = CurrentDate.Month.ToString().PadLeft(2, '0');
            DD = CurrentDate.Day.ToString().PadLeft(2, '0');
            WW = "";
            if (Format.IndexOf("WW") > -1)
            {
                int StartWeek = 4;
                DateTime CurrentStartDate = new DateTime(CurrentDate.Year, 1, 1);
                DateTime CurrentFirstWeekDate = getFirstWeekDate(CurrentStartDate, StartWeek);
                int DayCount = 0, WeekCount = 0, WeekDayCount = 0, WeekNum = 0, YearNum = 0, MonthNum = 0;
                if (CurrentDate >= CurrentFirstWeekDate)
                {
                    DateTime NextStartDate = new DateTime(CurrentDate.Year + 1, 1, 1);
                    DateTime NextFirstWeekDate = getFirstWeekDate(NextStartDate, StartWeek);
                    if (CurrentDate >= NextFirstWeekDate)
                    {
                        DayCount = (CurrentDate - NextFirstWeekDate).Days + 1;
                        YearNum = NextStartDate.Year;
                        MonthNum = NextStartDate.Month;
                    }
                    else
                    {
                        DayCount = (CurrentDate - CurrentFirstWeekDate).Days + 1;
                        YearNum = CurrentStartDate.Year;
                        MonthNum = CurrentStartDate.Month;
                    }
                }
                else
                {
                    DateTime PreviouStartDate = new DateTime(CurrentDate.Year - 1, 1, 1);
                    DateTime PreviouFirstWeekDate = getFirstWeekDate(PreviouStartDate, StartWeek);
                    DayCount = (CurrentDate - PreviouFirstWeekDate).Days + 1;
                    YearNum = PreviouStartDate.Year;
                    MonthNum = PreviouStartDate.Month;
                }
                WeekCount = (int)Math.Floor(DayCount / 7.0);
                WeekDayCount = DayCount % 7;
                WeekNum = WeekCount + (int)Math.Ceiling(WeekDayCount / 7.0);
                YYYY = YearNum.ToString();
                YY = YYYY.Substring(2, 2);
                MM = MonthNum.ToString().PadLeft(2, '0');
                WW = WeekNum.ToString().PadLeft(2, '0');
            }

            return Format.Replace("YYYY", YYYY).Replace("YY", YY).Replace("MM", MM).Replace("DD", DD).Replace("WW", WW);
        }
        /// <summary>
        /// 获取当前日期第一周 开始的日期
        /// </summary>
        /// <param name="StartDate">计算日期</param>
        /// <param name="StartWeek">起始周期</param>
        /// <returns>第一周日期</returns>
        public DateTime getFirstWeekDate(DateTime StartDate, int StartWeek = 4)
        {
            int CurrentWeek = (int)StartDate.DayOfWeek == 0 ? 7 : (int)StartDate.DayOfWeek;
            int CurrentWeekDiff = CurrentWeek <= StartWeek ? -CurrentWeek + 1 : 7 - CurrentWeek + 1;
            DateTime CurrentFirstWeekDate = StartDate.AddDays(CurrentWeekDiff);
            return CurrentFirstWeekDate;
        }

 

实现Demo效果: 

 

posted @ 2018-06-23 14:50  pcbren  阅读(1970)  评论(0编辑  收藏  举报