Condition:

There is a current date - DateTime current = DateTime.Now;

There is a date of birth - DateTime BirthDate = new DateTime(1970, 04, 06);

Task: Display a variable for calculating the elapsed time from BirthDate to current

  • 2
    (BirthDate - current) .TotalDays - Dmitry Chistik
  • @Dmitry Chistik: Why not as an answer? - VladD
  • @VladD give points to others. To be honest, I'm a very lazy person = ( - Dmitry Chistik
  • @DmitryChistik, I'm lazy too, but when bad answers are posted, I can't be silent :) - Andrey NOP
  • one
    In case you try to calculate age in years - How to calculate age in C #? . Just pay attention - it is impossible to calculate the age in the format yy, mm, dd - because month is not a constant value. - PashaPash

2 answers 2

Just subtract the date of birth from the current date and get the result in TimeSpan :

 var t = BirthDate - current; 

The DateTime structure has a specially overloaded operator for it: public static TimeSpan operator - (DateTime d1, DateTime d2)

  • Thank you, suitable. It remains to finish the method to output the date in the format (yy, mm, dd). - Rifter
  • one
    @Rifter: This method already exists and is called ToString . Just pick the right format string. - VladD
  • @VladD exactly, thanks - Rifter
  • one
    @VladD for TimeSpan is not formatted in years / months :) - PashaPash
  • @PashaPash, by the way, yes - you'll have to compile manually: Strings of the standard TimeSpan format and Strings of the custom TimeSpan format . - Andrei NOP

Translate both dates in the timestamp, then subtract the birth date from the current one https://stackoverflow.com/questions/17632584/how-to-get-the-unix-timestamp-in-c-sharp

You will get the result in milliseconds, then proceed according to circumstances.