您的位置:首页 > 其它

精确计算日期物理差(计算给定的两个日期间的物理年月日时分秒差)

2010-12-23 12:52 465 查看
没什么原因,就是突然想到了,在网上找了许久,都是大概的算法(年按365天算,月按30天算)都不是精确的算法,所以就写了一个

其实这个算法中年,小时,分,秒,都很容易计算,只是由于月份不同,每个月天的天数可能不同造成月与天的计算比较困难。

先上图



主要代码:

using System;

//作者:Corelly
//联系方式:corelly@163.com
//日期:2010.12.23
//你可以免费使用或修改以下代码,但请保留此段信息。
namespace CalcuPhyDateTime
{
struct PhyIntervalDateTime
{
private int phyYears;
private int phyMonths;
private int phyDays;
private int phyHours;
private int phyMinutes;
private int phySeconds;
#region 属性
/// <summary>
/// 物理年差
/// </summary>
public int PhyYears
{
get
{
return phyYears;
}
}
/// <summary>
/// 物理月差
/// </summary>
public int PhyMonths
{
get
{
return phyMonths;
}
}
/// <summary>
/// 物理天差
/// </summary>
public int PhyDays
{
get
{
return phyDays;
}
}
/// <summary>
/// 物理时差
/// </summary>
public int PhyHours
{
get
{
return phyHours;
}
}
/// <summary>
/// 物理分差
/// </summary>
public int PhyMinutes
{
get
{
return phyMinutes;
}
}
/// <summary>
/// 物理秒差
/// </summary>
public int PhySeconds
{
get
{
return phySeconds;
}
}
#endregion 属性
#region 函数
/// <summary>
/// 计算两个日期的物年月日时分秒差
/// </summary>
/// <param name="sDate1"></param>
/// <param name="sDate2"></param>
public void CalcuPhyDateTime(string sDate1, string sDate2)
{
DateTime outDateTime, Date1, Date2;
//判断给定的参照日期是否合法
if (!DateTime.TryParse(sDate1, out outDateTime))
{
throw new Exception("给定的参照日期不是一个有效的日期,请重新输入!");
return;
}
//判断给定的计算的日期是否合法
if (!DateTime.TryParse(sDate2, out outDateTime))
{
throw new Exception("给定的计算日期不是一个有效的日期,请重新输入!");
return;
}
//判断给定的计算日期是否比参照日期小
Date1 = Convert.ToDateTime(sDate1);
Date2 = Convert.ToDateTime(sDate2);
if (Date2 < Date1)
{
throw new Exception("计算日期必须大于给定的参照日期!");
return;
}
int Years, Months, Days, Hours, Minutes, Seconds; //需要的结果
int Year1, Month1, Day1, Hour1, Minute1, Second1; //参照日期的相关数值
int Year2, Month2, Day2, Hour2, Minute2, Second2; //计算日期的相关数值
Year1 = Date1.Year;
Month1 = Date1.Month;
Day1 = Date1.Day;
Hour1 = Date1.Hour;
Minute1 = Date1.Minute;
Second1 = Date1.Second;
Year2 = Date2.Year;
Month2 = Date2.Month;
Day2 = Date2.Day;
Hour2 = Date2.Hour;
Minute2 = Date2.Minute;
Second2 = Date2.Second;
//年
Years = Year2 - Year1;
//月
if (Month2 - Month1 < 0)
{
Months = 12 + Month2 - Month1;
Years--;
}
else
{
Months = Month2 - Month1;
}
//天
if (Day2 - Day1 < 0)
{
Days = Day2 + DateTime.DaysInMonth(Date1.Year, Date1.Month) - Day1;
Months--;
}
else
{
Days = Day2 - Day1;
}
//时
if (Hour2 - Hour1 < 0)
{
Hours = 24 + Hour2 - Hour1;
Days--;
}
else
{
Hours = Hour2 - Hour1;
}
//分
if (Minute2 - Minute1 < 0)
{
Minutes = 60 + Minute2 - Minute1;
Hours--;
}
else
{
Minutes = Minute2 - Minute1;
}
//秒
if (Second2 - Second1 < 0)
{
Seconds = 60 + Second2 - Second1;
Minutes--;
}
else
{
Seconds = Second2 - Second1;
}
phyYears = Years;
phyMonths = Months;
phyDays = Days;
phyHours = Hours;
phyMinutes = Minutes;
phySeconds = Seconds;
}
#endregion 函数
}
}

调用代码:

string sDate1 = textBox1.Text;
string sDate2 = textBox2.Text;
PhyIntervalDateTime phyIntervalDateTime = new PhyIntervalDateTime();
try
{
phyIntervalDateTime.CalcuPhyDateTime(sDate1, sDate2);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
string ResultStr = "计算日期{" + sDate2 + "}与参照日期{" + sDate1 + "}相差:" +
phyIntervalDateTime.PhyYears.ToString() + "年" +
phyIntervalDateTime.PhyMonths.ToString() + "月" +
phyIntervalDateTime.PhyDays.ToString() + "日" +
phyIntervalDateTime.PhyHours.ToString() + "时" +
phyIntervalDateTime.PhyMinutes.ToString() + "分" +
phyIntervalDateTime.PhySeconds.ToString() + "秒";
MessageBox.Show(ResultStr);

完整示例下载:http://download.csdn.net/source/2931339
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: