/// <summary> /// 获取本周的阳历日期列表(可能跨月) /// </summary> /// <returns>本周的阳历日期列表</returns> public List <Solar> getDays() { Solar firstDay = getFirstDay(); List <Solar> l = new List <Solar>(); l.Add(firstDay); for (int i = 1; i < 7; i++) { l.Add(firstDay.next(i)); } return(l); }
public void testNext() { Solar date = new Solar(2020, 1, 23); Assert.AreEqual("2020-01-24", date.next(1).ToString(), "LunarCore.Solar.next 有错。"); // 仅工作日,跨越春节假期 Assert.AreEqual("2020-02-03", date.next(1, true).ToString(), "LunarCore.Solar.next 有错。"); date = new Solar(2020, 2, 3); Assert.AreEqual("2020-01-31", date.next(-3).ToString(), "LunarCore.Solar.next 有错。"); // 仅工作日,跨越春节假期 Assert.AreEqual("2020-01-21", date.next(-3, true).ToString(), "LunarCore.Solar.next 有错。"); date = new Solar(2020, 2, 9); Assert.AreEqual("2020-02-15", date.next(6).ToString(), "LunarCore.Solar.next 有错。"); // 仅工作日,跨越周末 Assert.AreEqual("2020-02-17", date.next(6, true).ToString(), "LunarCore.Solar.next 有错。"); date = new Solar(2020, 1, 17); Assert.AreEqual("2020-01-18", date.next(1).ToString(), "LunarCore.Solar.next 有错。"); // 仅工作日,周日调休按上班算 Assert.AreEqual("2020-01-19", date.next(1, true).ToString(), "LunarCore.Solar.next 有错。"); }
/// <summary> /// 获取本月的阳历日期列表 /// </summary> /// <returns>阳历日期列表</returns> public List <Solar> getDays() { List <Solar> l = new List <Solar>(31); Solar d = new Solar(year, month, 1); l.Add(d); int days = SolarUtil.getDaysOfMonth(year, month); for (int i = 1; i < days; i++) { l.Add(d.next(i)); } return(l); }
/// <summary> /// 周推移 /// </summary> /// <param name="weeks">推移的周数,负数为倒推</param> /// <param name="separateMonth">是否按月单独计算</param> /// <returns>推移后的阳历周</returns> public SolarWeek next(int weeks, bool separateMonth) { if (0 == weeks) { return(new SolarWeek(year, month, day, start)); } if (separateMonth) { int n = weeks; DateTime c = new DateTime(year, month, day); SolarWeek week = new SolarWeek(c, start); int m = this.month; bool plus = n > 0; while (0 != n) { c = c.AddDays(plus ? 7 : -7); week = new SolarWeek(c, start); int weekMonth = week.getMonth(); if (m != weekMonth) { int index = week.getIndex(); if (plus) { if (1 == index) { Solar firstDay = week.getFirstDay(); week = new SolarWeek(firstDay.getYear(), firstDay.getMonth(), firstDay.getDay(), start); weekMonth = week.getMonth(); } else { c = new DateTime(week.getYear(), week.getMonth(), 1); week = new SolarWeek(c, start); } } else { int size = SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start); if (size == index) { Solar firstDay = week.getFirstDay(); Solar lastDay = firstDay.next(6); week = new SolarWeek(lastDay.getYear(), lastDay.getMonth(), lastDay.getDay(), start); weekMonth = week.getMonth(); } else { c = new DateTime(week.getYear(), week.getMonth(), SolarUtil.getDaysOfMonth(week.getYear(), week.getMonth())); week = new SolarWeek(c, start); } } m = weekMonth; } n -= plus ? 1 : -1; } return(week); } else { DateTime c = new DateTime(year, month, day); c = c.AddDays(weeks * 7); return(new SolarWeek(c, start)); } }