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效果:
作者:pcbren 微信号:yadnfku QQ号: 254566449
博客地址:https://www.cnblogs.com/pcbren/
声明:本博客原创文字只代表本人工作中在某一时间内总结的观点或结论,与本人所在单位没有直接利益关系。非商业,未授权,贴子请以现状保留,转载时必须保留此段声明,且在文章页面明显位置给出原文连接。
如果大家感觉我的博文对大家有帮助,请推荐支持一把。