国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#日期間隔計算示例

2020-01-24 02:48:57
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

/// <summary>
/// 計算日期的間隔(靜態(tài)類)
/// </summary>
public static class dateTimeDiff
{
#region 計算日期間隔
/// <summary>
/// 計算日期間隔
/// </summary>
/// <param name="d1">要參與計算的其中一個日期字符串</param>
/// <param name="d2">要參與計算的另一個日期字符串</param>
/// <returns>一個表示日期間隔的TimeSpan類型</returns>
public static TimeSpan toResult(string d1, string d2)
{
try
{
DateTime date1 = DateTime.Parse(d1);
DateTime date2 = DateTime.Parse(d2);
return toResult(date1, date2);
}
catch
{
throw new Exception("字符串參數(shù)不正確!");
}
}
#endregion

#region 計算日期間隔
/// <summary>
/// 計算日期間隔
/// </summary>
/// <param name="d1">要參與計算的其中一個日期</param>
/// <param name="d2">要參與計算的另一個日期</param>
/// <returns>一個表示日期間隔的TimeSpan類型</returns>
public static TimeSpan toResult(DateTime d1, DateTime d2)
{
TimeSpan ts;
if (d1 > d2)
{
ts = d1 - d2;
}
else
{
ts = d2 - d1;
}
return ts;
}
#endregion

#region 計算日期間隔
/// <summary>
/// 計算日期間隔
/// </summary>
/// <param name="d1">要參與計算的其中一個日期字符串</param>
/// <param name="d2">要參與計算的另一個日期字符串</param>
/// <param name="drf">決定返回值形式的枚舉</param>
/// <returns>一個代表年月日的int數(shù)組,具體數(shù)組長度與枚舉參數(shù)drf有關(guān)</returns>
public static int[] toResult(string d1, string d2, diffResultFormat drf)
{
try
{
DateTime date1 = DateTime.Parse(d1);
DateTime date2 = DateTime.Parse(d2);
return toResult(date1, date2, drf);
}
catch
{
throw new Exception("字符串參數(shù)不正確!");
}
}
#endregion

#region 計算日期間隔
/// <summary>
/// 計算日期間隔
/// </summary>
/// <param name="d1">要參與計算的其中一個日期</param>
/// <param name="d2">要參與計算的另一個日期</param>
/// <param name="drf">決定返回值形式的枚舉</param>
/// <returns>一個代表年月日的int數(shù)組,具體數(shù)組長度與枚舉參數(shù)drf有關(guān)</returns>
public static int[] toResult(DateTime d1, DateTime d2, diffResultFormat drf)
{
#region 數(shù)據(jù)初始化
DateTime max;
DateTime min;
int year;
int month;
int tempYear, tempMonth;
if (d1 > d2)
{
max = d1;
min = d2;
}
else
{
max = d2;
min = d1;
}
tempYear = max.Year;
tempMonth = max.Month;
if (max.Month < min.Month)
{
tempYear--;
tempMonth = tempMonth + 12;
}
year = tempYear - min.Year;
month = tempMonth - min.Month;
#endregion
#region 按條件計算
if (drf == diffResultFormat.dd)
{
TimeSpan ts = max - min;
return new int[] { ts.Days };
}
if (drf == diffResultFormat.mm)
{
return new int[] { month + year * 12 };
}
if (drf == diffResultFormat.yy)
{
return new int[] { year };
}
return new int[] { year, month };
#endregion
}
#endregion
}
#region 關(guān)于返回值形式的枚舉
/// <summary>
/// 關(guān)于返回值形式的枚舉
/// </summary>
public enum diffResultFormat
{
/// <summary>
/// 年數(shù)和月數(shù)
/// </summary>
yymm,
/// <summary>
/// 年數(shù)
/// </summary>
yy,
/// <summary>
/// 月數(shù)
/// </summary>
mm,
/// <summary>
/// 天數(shù)
/// </summary>
dd,
}
#endregion

復(fù)制代碼 代碼如下:

DateTime sDate = Convert.ToDateTime("2014-1-16");
DateTime eDate = Convert.ToDateTime("2014-2-16");
int month = dateTimeDiff.toResult(sDate, eDate, diffResultFormat.mm)[0];

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 澄城县| 资兴市| 方山县| 裕民县| 罗江县| 宿州市| 寿宁县| 大足县| 普兰店市| 扶余县| 栖霞市| 宾阳县| 鄂尔多斯市| 五峰| 宁蒗| 辰溪县| 建德市| 抚宁县| 仪陇县| 九江市| 巍山| 砀山县| 佛冈县| 扬中市| 泸州市| 邢台县| 南岸区| 武威市| 屯留县| 阳江市| 南溪县| 巨鹿县| 河东区| 玛纳斯县| 鸡西市| 山阴县| 克东县| 肇东市| 夏津县| 文昌市| 木兰县|