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 / + ;
From:http://tw.wingwit.com/Article/program/net/201311/11629.html