It does not gracefully handle negative timespans, unlike the other
algorithm. It also doesn't use its own date arithmetic, instead relying
upon the standard library for that.
static void Main(string[] args)
{
/ /Check year difference between two dates
DateTime zeroTime = new DateTime(1, 1, 1);
DateTime startDate = Convert.ToDateTime(Console.ReadLine());
DateTime endDate = Convert.ToDateTime(Console.ReadLine());
if (startDate < endDate)
{
DateTime a = new DateTime(startDate.Year, startDate.Month, startDate.Day);
DateTime b = new DateTime(endDate.Year, endDate.Month, endDate.Day);
TimeSpan span = b - a;
// Because we start at year 1 for the Gregorian
// calendar, we must subtract a year here.
int years = (zeroTime + span).Year - 1;
// 1, where my other algorithm resulted in 0.
Console.WriteLine("Yrs elapsed: " + years);
}
Console.ReadLine();
}
static void Main(string[] args)
{
/ /Check year difference between two dates
DateTime zeroTime = new DateTime(1, 1, 1);
DateTime startDate = Convert.ToDateTime(Console.ReadLine());
DateTime endDate = Convert.ToDateTime(Console.ReadLine());
if (startDate < endDate)
{
DateTime a = new DateTime(startDate.Year, startDate.Month, startDate.Day);
DateTime b = new DateTime(endDate.Year, endDate.Month, endDate.Day);
TimeSpan span = b - a;
// Because we start at year 1 for the Gregorian
// calendar, we must subtract a year here.
int years = (zeroTime + span).Year - 1;
// 1, where my other algorithm resulted in 0.
Console.WriteLine("Yrs elapsed: " + years);
}
Console.ReadLine();
}
No comments:
Post a Comment