PCB 生产周期计算逻辑与代码实现
Posted pcbren
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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效果:
以上是关于PCB 生产周期计算逻辑与代码实现的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud系列十一:SpringCloudStream(SpringCloudStream 简介创建消息生产者创建消息消费者自定义消息通道分组与持久化设置 RoutingKey)(代码片段