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效果: 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值