0

I am working with the following code:

    static void Main(string[] args)
    {
        Dates();
        Console.ReadLine();        

    }

    public static void Dates()
    {

        Console.WriteLine("Enter any date: ");
        DateTime firstDate = DateTime.Parse(Console.ReadLine());
        Console.WriteLine("Enter another date: ");
        DateTime secondDate = DateTime.Parse(Console.ReadLine());

        TimeSpan diff = firstDate - secondDate;

        Console.WriteLine("Days : " + diff.TotalDays.ToString());
        Console.WriteLine("Hours : " + diff.TotalHours.ToString());
        Console.WriteLine("Minutes : " + diff.TotalMinutes.ToString());


    }

I haven't been able to figure out how to calculate the difference in months and years between the two dates. Can anyone help? Also, how can I make the program return a positive integer every time?

  • 2
    How would you do it if you were counting on your fingers? Do it that way. – Andrew Morton Oct 10 '17 at 17:17
  • For the final part of the question: `TimeSpan diff = (firstDate - secondDate).Duration()`. – Andrew Morton Oct 10 '17 at 17:20
  • why is reason to return always positive, when do you want to send this data? – Enrique YC Oct 10 '17 at 17:20
  • I think he is talking about the application's exit code. – JuanR Oct 10 '17 at 17:25
  • For the app's exit code, use `Environment.Exit(exitCode)`. – JuanR Oct 10 '17 at 17:29
  • As far as I can tell the OP wants to display the time difference as positive numbers. (since the dates are input in any order, but the difference is found as `(firstDate - secondDate)`. This could lead to negative differences is time if the first date is less than the second date. For example, OP would want `10/1/17 - 10/10/17` to be `9` days as opposed to `-9` (which would be how the OP's code currently does it) – maccettura Oct 10 '17 at 17:33
  • @maccettura oh man, wish you hadn't deleted your answer (at least, I think it was you who answered). I think the refactoring was a good move (had value) and I think you were most of the way there to having it answered in a very detailed manner as well. – Robert Oct 10 '17 at 18:14
  • 2
    @Robert yeah it was me. The refactor had value, but it wasn't really an answer to the question. Without knowing more from the OP I cannot really answer. – maccettura Oct 10 '17 at 18:20

1 Answers1

0

This is an answer to the first part of the question, calculating difference between two dates in years and months (and days, why not). The question is somewhat ambiguous so I made a few assumptions.

Like, I take the 'difference' to be what we generally mean when we calculate age. As in, if start date is 5/12/2014 and end date is 2/16/2017, then I calculate the difference to be 2 years, 9 months, and 3 days. You get the drift.

Second, I don't take into account the time of day. As in if I created a DateTime object with specifying year, month and day only.

So here goes:

public static void DateTimeDiff(DateTime dt1, DateTime dt2, out int years, out int months, out int days)
{
    DateTime start = dt1 < dt2 ? dt1 : dt2;
    DateTime end = dt2 > dt1 ? dt2 : dt1;

    years = 0;
    months = 0;
    days = 0;

    while (start.AddYears(1) < end)
    {
        start = start.AddYears(1);
        years++;
    }

    while (start.AddMonths(1) < end)
    {
        start = start.AddMonths(1);
        months++;
    }

    while (start.AddDays(1) < end)
    {
        start = start.AddDays(1);
        days++;
    }
}
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
Sach
  • 10,091
  • 8
  • 47
  • 84