3

Imagine I have two DateTime objects, one representing today's date, the other representing the user's supplied date of birth.

I want to do a simple calculation difference between the two dates and output a string in the following format:

17 Years - 2 Months - 18 Days

Them numbers are just examples. How can I achieve this in C#?

EDIT:

Yep I've tried all sorts of combinations, but I was only able to get the Years difference like so:

DateTime userDate = SuppliedDate;
int userAge = DateTime.Now.Year - userDate.Year;
DateTime today = DateTime.Now;
if (userDate > today.AddYears(-userAge))
{
    userAge--;
}
Age.Text = userAge.ToString() + " years";

Thank You.

J86
  • 14,345
  • 47
  • 130
  • 228

5 Answers5

6

Simply convert the TimeSpan back to a Datetime:

        var test = DateTime.Now;
        var test2 = DateTime.Now;
        var result = test - test2;
        DateTime resultDate = DateTime.MinValue + result;
Christian Sauer
  • 10,351
  • 10
  • 53
  • 85
2

This is a trickier problem than it first seems; Years have a variable number of days (Leap Years) as do months (28,29,30 or 31). Therefore, in order to get the output you are wanting, you have to step over each year and month between birth and current dates, checking the leap year status, the number of days in each month and incrementing as you go.

The first step, as has been pointed out in other answers, is to use the TimeSpan struct to obtain the total number of days old a person is. This is the easy part

var dob = new DateTime(1962,5,20);
var age = DateTime.Today.Subtract(dob.Date); // May as well use midnight's
var totalDays = age.TotalDays;

From there, you need to start with the year of birth (1962 in this example) and begin counting years until the current year. Each time you pass a year, subtract 365 days from the total (or 366 if its a leap year).

When you reach within a year of the current date, begin counting months and again subtract the right number from the remaining total days.

When you reach within a month of the current date, the remainder you have left is the final part of your string.

I will work an a working version of the above pseudo-code as I get a chance, but I hope this gives you a pointer in the right direction.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

When you subtract a Date from another in C#, you get back a TimeSpan structure.

TimeSpans are not designed to give you months or years - even with a custom format string.

This means you will have to manually calculate years and months based on the available properties which at the largest are days, but go to much lower levels of precision.

Fenton
  • 241,084
  • 71
  • 387
  • 401
0

Try if this suites your needs:

DateTime dt3 = new DateTime((dt2 - dt1).Ticks);

You will have to correct for all values (year, month, day) being one too much.

JeffRSon
  • 10,404
  • 4
  • 26
  • 51
-4

You can use somthing like this:

var userDate = new DateTime(1980, 5, 3);
var dateDif = DateTime.Now.Subtract(userDate);
string output = dateDif.ToString("yy Years - MM Months - dd Days");
Oleg Ignatov
  • 877
  • 2
  • 8
  • 22
  • Did you actually try this? [It doesn't work!](http://rextester.com/UKFQO41334) – Jamiec May 17 '13 at 08:43
  • Pheww did someone upvote this , does this work ? Diff of date is a `Timespan` and one with Years in it ! ! – V4Vendetta May 17 '13 at 08:45
  • @V4Vendetta - Yes, someone did upvote this trash. I countered it with a down and a demonstration that the code actually doesnt compile! – Jamiec May 17 '13 at 08:53
  • You cannot format a TimeSpan like that. Causes a FormatException. – base2 Jun 21 '14 at 09:12