1. 程式人生 > >PCB 生產周期計算邏輯與代碼實現

PCB 生產周期計算邏輯與代碼實現

add 圖片 etime datetime PC bubuko In iou string

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

技術分享圖片

PCB 生產周期計算邏輯與代碼實現