Author:小山 from:Cnblogs 常见日期方法荟萃 一如何获得当月有多少天 int m=SystemDateTimeDaysInMonth(System DateTimeNowYearSystemDateTimeNowMonth); 二日期型格式处理通用方法 在webconfig中配置如下 在globalasax中protected void Application_BeginRequest(Object sender EventArgs e){Thread currentThread = ThreadCurrentThread;CultureInfo cul = currentThreadCurrentCultureClone() as CultureInfo;culDateTimeFormatShortDatePattern= BLLFacadeCommonGetShortDatePattern();culDateTimeFormatLongDatePattern= BLLFacadeCommonGetLongDatePattern();culDateTimeFormatShortTimePattern= BLLFacadeCommonGetShortTimePattern();culDateTimeFormatLongTimePattern= BLLFacadeCommonGetLongTimePattern();currentThreadCurrentCulture = cul;} 在业务逻辑层中 public static string GetShortDatePattern(){return SystemConfigurationConfigurationSettingsAppSettings[ShortDatePattern];}public static string GetLongDatePattern(){return SystemConfigurationConfigurationSettingsAppSettings[LongDatePattern];}public static string GetShortTimePattern(){return SystemConfigurationConfigurationSettingsAppSettings[ShortTimePattern];}public static string GetLongTimePattern(){return SystemConfigurationConfigurationSettingsAppSettings[LongTimePattern];} 然后在其他地方正常调用就可以了如果需要修改格式只需要修改webconfig中的且可以保证整个系统中的所有格式都是一致的 三在中怎么样计算两个日期相差的年月份日期小时分钟 妙等在中怎么样计算两个日期相差的年月份日期小时分钟 妙等#region 在中怎么样计算两个日期相差的年月份日期小时分钟 妙等 // 调用// DateTime a=ConvertToDateTime( :);// DateTime b=ConvertToDateTime( : );// double d=BllCommonDateDiff(BllCommonEnumDateComparedayab);// ResponseWrite(dToString(f));//四捨五入 public enum EnumDateCompare { year = month = day = hour = minute = second = } public static double DateDiff(EnumDateCompare howtocompare SystemDateTime startDate SystemDateTime endDate) { double diff=; SystemTimeSpan TS = new SystemTimeSpan(endDateTicksstartDateTicks); switch (howtocompare) { case EnumDateCompareyear: diff = ConvertToDouble(TSTotalDays/); break; case EnumDateComparemonth: diff = ConvertToDouble((TSTotalDays/)*); break; case EnumDateCompareday: diff = ConvertToDouble(TSTotalDays); break; case EnumDateComparehour: diff = ConvertToDouble(TSTotalHours); break; case EnumDateCompareminute: diff = ConvertToDouble(TSTotalMinutes); break; case EnumDateComparesecond: diff = ConvertToDouble(TSTotalSeconds); break; } return diff; } #endregion四获取某月的实际工作日(即不包括周六日)//调用//int days =getDays(SystemDateTimeNow));private int getDays(SystemDateTime date){ int m=SystemDateTimeDaysInMonth(dateYeardateMonth); int mm=; for(int i=;i<=m;i++) { SystemDateTime date=ConvertToDateTime(dateYear++dateMonth++i); switch (dateDayOfWeek) { case SystemDayOfWeekMonday: case SystemDayOfWeekThursday: case SystemDayOfWeekTuesday: case SystemDayOfWeekWednesday: case SystemDayOfWeekFriday: mm=mm+; break; } } return mm;} 五获得任意两日期之间的有效工作日(不包括周六日) 获得任意两日期之间的有效工作日(不包括周六日)#region 获得任意两日期之间的有效工作日(不包括周六日) //调用 //DateTime date=ConvertToDateTime(); //DateTime date=ConvertToDateTime(); //int days =getDays(datedate); private int getDays(SystemDateTime dateSystemDateTime date) { string m=DateDiff(EnumDateComparedaydatedate)ToString(f); int mm=; for(int i=;i<=ConvertToInt(m);i++) { SystemDateTime date=ConvertToDateTime(dateAddDays(i)); switch (dateDayOfWeek) { case SystemDayOfWeekMonday: case SystemDayOfWeekThursday: case SystemDayOfWeekTuesday: case SystemDayOfWeekWednesday: case SystemDayOfWeekFriday: mm=mm+; break; } } return mm; } #endregion六格式输出 格式输出#region 格式输出 private void Page_Load(object sender SystemEventArgs e) { SystemGlobalizationDateTimeFormatInfo myDTFI = new System GlobalizationCultureInfo( enUS false )DateTimeFormat;//中国用zhcn DateTime myDT =SystemDateTimeNow; ResponseWrite(myDTToString(fmyDTFI)); /**//**//**//* This code produces the following output FORMAT enUS EXAMPLE CHAR VALUE OF ASSOCIATED PROPERTY IF ANY d // M/d/yyyy (ShortDatePattern) D Thursday January dddd MMMM dd yyyy (LongDatePattern) f Thursday January : AM F Thursday January :: AM dddd MMMM dd yyyy h:mm:ss tt (FullDateTimePattern) g // : AM G // :: AM m January MMMM dd (MonthDayPattern) M January MMMM dd (MonthDayPattern) r Thu Jan :: GMT ddd dd MMM yyyy HH:mm:ss GMT (RFCPattern) R Thu Jan :: GMT ddd dd MMM yyyy HH:mm:ss GMT (RFCPattern) s T:: yyyyMMddTHH:mm:ss (SortableDateTimePattern) t : AM h:mm tt (ShortTimePattern) T :: AM h:mm:ss tt (LongTimePattern) u ::Z yyyyMMdd HH:mm:ssZ (UniversalSortableDateTimePattern) U Thursday January :: AM y January MMMM yyyy (YearMonthPattern) Y January MMMM yyyy (YearmonthPattern) */ } #endregion 七获得本周的周六和周日ConvertDateToWeek#region ConvertDateToWeek public static void ConvertDateToWeek(DateTime date out DateTime firstdateout DateTime lastdate) { DateTime first=SystemDateTimeNow; DateTime last=SystemDateTimeNow; switch (dateDayOfWeek) { case SystemDayOfWeekMonday: first=dateAddDays(); last=dateAddDays(); break; case SystemDayOfWeekTuesday: first=dateAddDays(); last=dateAddDays(); break; case SystemDayOfWeekWednesday: first=dateAddDays(); last=dateAddDays(); break; case SystemDayOfWeekThursday: first=dateAddDays(); last=dateAddDays(); break; case SystemDayOfWeekFriday: first=dateAddDays(); last=dateAddDays(); break; case SystemDayOfWeekSaturday: first=dateAddDays(); last=date; break; case SystemDayOfWeekSunday: first=date; last=dateAddDays(); break; } firstdate=first; lastdate=last; } #endregion //调用DateTime firstdate=SystemDateTimeNow; DateTime lastdate=SystemDateTimeNow;ConvertDateToWeek(dateout firstdateout lastdate);八获得当前日期是该年度的第几周DateTime dt = ConvertToDateTime();int weeks = dtDayOfYear / + ; |