DateTime dt1 = new DateTime(2008, 12, 31);
DateTime dt2 = new DateTime();
dt2 = DateTime.Now;
TimeSpan ts = new TimeSpan(dt2.Ticks - dt1.Ticks);
//相差天數(未滿一天捨去,return int type)
Response.Write(Convert.ToString( ts.Days ));
//相差天數(未滿一天亦計入,return double type)
Response.Write(Convert.ToString( ts.TotalDays ));
//相差小時數(return double type)
Response.Write(Convert.ToString( ts.TotalHours ));
//相差秒數(return double type)
Response.Write(Convert.ToString( ts.TotalMinutes ));
public static int GetTimeDiff(string strFrom, string strTo, string strType)
{
DateTime dtStart = DateTime.Parse(strFrom);
DateTime dtEnd = DateTime.Parse(strTo);
if (strType == "D")
{
//使用TimeSpan提供的Days屬性
TimeSpan ts = (dtEnd - dtStart);
int iDays = ts.Days + 1;
return iDays;
}
else if (strType == "M")
{
int iMonths = dtEnd.Year * 12 + dtEnd.Month - (dtStart.Year * 12 + dtStart.Month) + 1;
return iMonths;
}
else return 0;
}
留言列表