星期几以及周,月份天数的计算(共用方法)
#region 计算星期几 /// <summary> /// //: 星期日至星期六的值为0-6 /// </summary> /// <param name="year"></param> /// <param name="month"></param> /// <param name="day"></param> /// <returns></returns> public static int GetWeeksOfDate(int year, int month, int day) { return GetWeeksOfDate(new DateTime(year, month, day)); } public static int GetWeeksOfDate(DateTime time) { DayOfWeek d = time.DayOfWeek; return Convert.ToInt32(d); } public static string GetWeeksOfDateForChinese(DateTime time) { int week = GetWeeksOfDate(time); string strWeek =""; if (week == 0) { strWeek = "日"; } else { strWeek =Tools.Number2Chinese(week); } return "周" + strWeek; } /// <summary> /// 星期一至星期日的值为1-7 /// </summary> /// <param name="time"></param> /// <returns></returns> public static int GetNumOfWeeks(DateTime time) { int t=GetWeeksOfDate(time); if(t==0) { t = 7; } return t; } #endregion
#region 获取指定年月的天数 /// <summary> /// 获取指定年月的天数 /// </summary> /// <param name="year"></param> /// <param name="month"></param> /// <returns></returns> public static int GetDaysOfMonth(int year, int month) { DateTime dtCur = new DateTime(year, month, 1); int days = dtCur.AddMonths(1).AddDays(-1).Day; return days; } #endregion