日期和月份计算未 return C# 中的预期输出
Day and Month calculation does not return expected output in C#
我想计算两天之间的时差..
这是我的代码:
private static string CalculateDayMonth(DateTime date1, DateTime date2)
{
int totalMonth = 0;
string returnValue = "";
if (date1 >= date2)
{
int days = date1.Day - date2.Day;
int months = date1.Month - date2.Month;
int years = date1.Year - date2.Year;
if (months < 0)
{
months += 12;
years -= 1;
}
if (days < 0)
{
int dayToSubtract = DateTime.DaysInMonth(date2.Year, date2.Month);
days = days + dayToSubtract;
months -= 1;
}
if (years > 0)
totalMonth = years * 12;
else
years = 0;
totalMonth += months;
if (totalMonth <= 0)
returnValue = string.Format(AppResources.Label50414, days.ToString());
else
returnValue = string.Format(AppResources.Label50415, totalMonth.ToString());
}
return returnValue;
}
我输入的日期是:
2015 年 1 月 2 日 14:02:47 下午 -
2014 年 12 月 15 日 16:14:50 下午
预期产量为 17 天
但实际输出是:18 天。
我也尝试了一些其他的例子。但这也给出了相同的输出。
请告诉我哪里做错了。
您的代码甚至 都不会计算您的小时、分钟和秒部分。没有小时部分,there are 18
days between January 2 and December 15 基于他们的午夜。
但是您的 link 也 计算小时、分钟和秒部分。由于 12 月 15 日的小时部分比 1 月 2 日高,因此这些日期之间的差异将小于 18
天。这就是它们不同的原因。
看起来你只需要使用简单的 TimeSpan
like;
var dt1 = new DateTime(2015, 1, 2, 14, 2, 47);
var dt2 = new DateTime(2014, 12, 15, 16, 14, 50);
var ts = dt1 - dt2;
Console.WriteLine(ts.Days); // 17
Console.WriteLine(ts.Hours); // 21
Console.WriteLine(ts.Minutes); // 47
Console.WriteLine(ts.Seconds); // 57
按格式;
Console.WriteLine("{0} days, {1} hours, {2} minutes and {3} seconds",
ts.Days,
ts.Hours,
ts.Minutes,
ts.Seconds); // 17 days, 21 hours, 47 minutes and 57 seconds
private struct DateResult
{
public int years;
public int months;
public int days;
}
private DateResult Date_Difference(DateTime d1, DateTime d2)
{
DateResult d_r;
d_r.years = 0;
d_r.months = 0;
d_r.days = 0;
DateTime frm_date, to_Date;
int key=0;
if (d1 > d2)
{
frm_date = d2;
to_Date = d1;
}
else
{
frm_date = d1;
to_Date = d2;
}
if (frm_date.Day > to_Date.Day) //10 5 //7 24
{
key = month_day[frm_date.Month - 1];
if (key == -1)
{
if (DateTime.IsLeapYear(frm_date.Year))
key = 29;
else
key = 28;
}
}
if (key == 0)
{
d_r.days = to_Date.Day - frm_date.Day;
}
else
{
d_r.days = (to_Date.Day + key) - frm_date.Day;
key = 1;
}
if ((frm_date.Month+key) > to_Date.Month)
{
d_r.months = (to_Date.Month + 12) - (frm_date.Month + key);
key = 1;
}
else
{
d_r.months = to_Date.Month - (frm_date.Month+key);
key=0;
}
d_r.years = to_Date.Year - (frm_date.Year+key);
return d_r;
}
此 C# 代码..对我有用...我们还可以了解小时、分钟和秒
我想计算两天之间的时差..
这是我的代码:
private static string CalculateDayMonth(DateTime date1, DateTime date2)
{
int totalMonth = 0;
string returnValue = "";
if (date1 >= date2)
{
int days = date1.Day - date2.Day;
int months = date1.Month - date2.Month;
int years = date1.Year - date2.Year;
if (months < 0)
{
months += 12;
years -= 1;
}
if (days < 0)
{
int dayToSubtract = DateTime.DaysInMonth(date2.Year, date2.Month);
days = days + dayToSubtract;
months -= 1;
}
if (years > 0)
totalMonth = years * 12;
else
years = 0;
totalMonth += months;
if (totalMonth <= 0)
returnValue = string.Format(AppResources.Label50414, days.ToString());
else
returnValue = string.Format(AppResources.Label50415, totalMonth.ToString());
}
return returnValue;
}
我输入的日期是: 2015 年 1 月 2 日 14:02:47 下午 - 2014 年 12 月 15 日 16:14:50 下午
预期产量为 17 天
但实际输出是:18 天。
我也尝试了一些其他的例子。但这也给出了相同的输出。
请告诉我哪里做错了。
您的代码甚至 都不会计算您的小时、分钟和秒部分。没有小时部分,there are 18
days between January 2 and December 15 基于他们的午夜。
但是您的 link 也 计算小时、分钟和秒部分。由于 12 月 15 日的小时部分比 1 月 2 日高,因此这些日期之间的差异将小于 18
天。这就是它们不同的原因。
看起来你只需要使用简单的 TimeSpan
like;
var dt1 = new DateTime(2015, 1, 2, 14, 2, 47);
var dt2 = new DateTime(2014, 12, 15, 16, 14, 50);
var ts = dt1 - dt2;
Console.WriteLine(ts.Days); // 17
Console.WriteLine(ts.Hours); // 21
Console.WriteLine(ts.Minutes); // 47
Console.WriteLine(ts.Seconds); // 57
按格式;
Console.WriteLine("{0} days, {1} hours, {2} minutes and {3} seconds",
ts.Days,
ts.Hours,
ts.Minutes,
ts.Seconds); // 17 days, 21 hours, 47 minutes and 57 seconds
private struct DateResult
{
public int years;
public int months;
public int days;
}
private DateResult Date_Difference(DateTime d1, DateTime d2)
{
DateResult d_r;
d_r.years = 0;
d_r.months = 0;
d_r.days = 0;
DateTime frm_date, to_Date;
int key=0;
if (d1 > d2)
{
frm_date = d2;
to_Date = d1;
}
else
{
frm_date = d1;
to_Date = d2;
}
if (frm_date.Day > to_Date.Day) //10 5 //7 24
{
key = month_day[frm_date.Month - 1];
if (key == -1)
{
if (DateTime.IsLeapYear(frm_date.Year))
key = 29;
else
key = 28;
}
}
if (key == 0)
{
d_r.days = to_Date.Day - frm_date.Day;
}
else
{
d_r.days = (to_Date.Day + key) - frm_date.Day;
key = 1;
}
if ((frm_date.Month+key) > to_Date.Month)
{
d_r.months = (to_Date.Month + 12) - (frm_date.Month + key);
key = 1;
}
else
{
d_r.months = to_Date.Month - (frm_date.Month+key);
key=0;
}
d_r.years = to_Date.Year - (frm_date.Year+key);
return d_r;
}
此 C# 代码..对我有用...我们还可以了解小时、分钟和秒