Come faccio a calcolare l’età di qualcuno in C #?

Dato un DateTime rappresenta il compleanno di una persona, come faccio a calcolare la loro età in anni?

Una soluzione facile da capire e semplice.

 // Save today's date. var today = DateTime.Today; // Calculate the age. var age = today.Year - birthdate.Year; // Go back to the year the person was born in case of a leap year if (birthdate > today.AddYears(-age)) age--; 

Tuttavia, questo presuppone che tu stia cercando l’idea occidentale dell’età e non usando la resa dei conti dell’Asia orientale .

Questo è uno strano modo per farlo, ma se si formatta la data a yyyymmdd e si sottrae la data di nascita dalla data corrente, allora rilascia le ultime 4 cifre che hai l’età 🙂

Non conosco C #, ma credo che funzionerà in qualsiasi lingua.

 20080814 - 19800703 = 280111 

Rilascia le ultime 4 cifre = 28 .

Codice C #:

 int now = int.Parse(DateTime.Now.ToString("yyyyMMdd")); int dob = int.Parse(dateOfBirth.ToString("yyyyMMdd")); int age = (now - dob) / 10000; 

O in alternativa senza tutta la conversione di tipo sotto forma di un metodo di estensione. Controllo errori omesso:

 public static Int32 GetAge(this DateTime dateOfBirth) { var today = DateTime.Today; var a = (today.Year * 100 + today.Month) * 100 + today.Day; var b = (dateOfBirth.Year * 100 + dateOfBirth.Month) * 100 + dateOfBirth.Day; return (a - b) / 10000; } 

Non so come possa essere accettata la soluzione sbagliata. Lo snippet C # corretto è stato scritto da Michael Stum

Ecco uno snippet di test:

 DateTime bDay = new DateTime(2000, 2, 29); DateTime now = new DateTime(2009, 2, 28); MessageBox.Show(string.Format("Test {0} {1} {2}", CalculateAgeWrong1(bDay, now), // outputs 9 CalculateAgeWrong2(bDay, now), // outputs 9 CalculateAgeCorrect(bDay, now))); // outputs 8 

Ecco i metodi:

 public int CalculateAgeWrong1(DateTime birthDate, DateTime now) { return new DateTime(now.Subtract(birthDate).Ticks).Year - 1; } public int CalculateAgeWrong2(DateTime birthDate, DateTime now) { int age = now.Year - birthDate.Year; if (now < birthDate.AddYears(age)) age--; return age; } public int CalculateAgeCorrect(DateTime birthDate, DateTime now) { int age = now.Year - birthDate.Year; if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day)) age--; return age; } 

Non credo che nessuna delle risposte finora fornisca culture che calcolano l’età in modo diverso. Vedi, per esempio, Reckoning dell’Età dell’Asia Orientale contro quello dell’Occidente.

Qualsiasi risposta reale deve includere la localizzazione. In questo esempio il modello di strategia sarebbe probabilmente in ordine.

La semplice risposta a questo è applicare AddYears come mostrato di seguito perché questo è l’unico metodo nativo per aggiungere anni al 29 febbraio degli anni bisestili e ottenere il risultato corretto del 28 febbraio per gli anni comuni.

Alcuni ritengono che il 1 ° marzo sia il compleanno degli sposi, ma né. Net né alcuna regola ufficiale lo supporta, né la logica comune spiega perché alcuni nati a febbraio dovrebbero avere il 75% dei loro compleanni in un altro mese.

Inoltre, un metodo Age si presta ad essere aggiunto come estensione a DateTime . Con questo è ansible ottenere l’età nel modo più semplice ansible:

  1. Elemento dell’elenco

int age = birthDate.Age ();

 public static class DateTimeExtensions { ///  /// Calculates the age in years of the current System.DateTime object today. ///  /// The date of birth /// Age in years today. 0 is returned for a future date of birth. public static int Age(this DateTime birthDate) { return Age(birthDate, DateTime.Today); } ///  /// Calculates the age in years of the current System.DateTime object on a later date. ///  /// The date of birth /// The date on which to calculate the age. /// Age in years on a later day. 0 is returned as minimum. public static int Age(this DateTime birthDate, DateTime laterDate) { int age; age = laterDate.Year - birthDate.Year; if (age > 0) { age -= Convert.ToInt32(laterDate.Date < birthDate.Date.AddYears(age)); } else { age = 0; } return age; } } 

Ora esegui questo test:

 class Program { static void Main(string[] args) { RunTest(); } private static void RunTest() { DateTime birthDate = new DateTime(2000, 2, 28); DateTime laterDate = new DateTime(2011, 2, 27); string iso = "yyyy-MM-dd"; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Console.WriteLine("Birth date: " + birthDate.AddDays(i).ToString(iso) + " Later date: " + laterDate.AddDays(j).ToString(iso) + " Age: " + birthDate.AddDays(i).Age(laterDate.AddDays(j)).ToString()); } } Console.ReadKey(); } } 

L'esempio di data critica è questo:

Data di nascita: 2000-02-29 Data successiva: 2011-02-28 Età: 11

Produzione:

 { Birth date: 2000-02-28 Later date: 2011-02-27 Age: 10 Birth date: 2000-02-28 Later date: 2011-02-28 Age: 11 Birth date: 2000-02-28 Later date: 2011-03-01 Age: 11 Birth date: 2000-02-29 Later date: 2011-02-27 Age: 10 Birth date: 2000-02-29 Later date: 2011-02-28 Age: 11 Birth date: 2000-02-29 Later date: 2011-03-01 Age: 11 Birth date: 2000-03-01 Later date: 2011-02-27 Age: 10 Birth date: 2000-03-01 Later date: 2011-02-28 Age: 10 Birth date: 2000-03-01 Later date: 2011-03-01 Age: 11 } 

E per la data successiva 2012-02-28:

 { Birth date: 2000-02-28 Later date: 2012-02-28 Age: 12 Birth date: 2000-02-28 Later date: 2012-02-29 Age: 12 Birth date: 2000-02-28 Later date: 2012-03-01 Age: 12 Birth date: 2000-02-29 Later date: 2012-02-28 Age: 11 Birth date: 2000-02-29 Later date: 2012-02-29 Age: 12 Birth date: 2000-02-29 Later date: 2012-03-01 Age: 12 Birth date: 2000-03-01 Later date: 2012-02-28 Age: 11 Birth date: 2000-03-01 Later date: 2012-02-29 Age: 11 Birth date: 2000-03-01 Later date: 2012-03-01 Age: 12 } 

Il mio consiglio

 int age = (int) ((DateTime.Now - bday).TotalDays/365.242199); 

Sembra che l’anno cambi nella data giusta. (Riconosco fino a 107 anni)

Un’altra funzione, non da me ma trovata sul web e perfezionata un po ‘:

 public static int GetAge(DateTime birthDate) { DateTime n = DateTime.Now; // To avoid a race condition around midnight int age = n.Year - birthDate.Year; if (n.Month < birthDate.Month || (n.Month == birthDate.Month && n.Day < birthDate.Day)) age--; return age; } 

Solo due cose che mi vengono in mente: che dire delle persone di paesi che non usano il calendario gregoriano? DateTime.Now è nella cultura specifica del server, credo. Ho assolutamente 0 conoscenze circa il fatto di lavorare con calendari asiatici e non so se c'è un modo semplice per convertire le date tra calendari, ma nel caso ti stia chiedendo di quei ragazzi cinesi dell'anno 4660 🙂

Sono in ritardo per la festa, ma qui c’è una sola:

 int age = new DateTime(DateTime.Now.Subtract(birthday).Ticks).Year-1; 

2 I principali problemi da risolvere sono:

1. Calcola l’età esatta – in anni, mesi, giorni, ecc.

2. Calcola Generalmente l’età percepita – le persone di solito non si preoccupano di quanti anni hanno esattamente, a loro importa solo quando il loro compleanno nell’anno corrente è.


La soluzione per 1 è ovvia:

 DateTime birth = DateTime.Parse("1.1.2000"); DateTime today = DateTime.Today; //we usually don't care about birth time TimeSpan age = today - birth; //.NET FCL should guarantee this as precise double ageInDays = age.TotalDays; //total number of days ... also precise double daysInYear = 365.2425; //statistical value for 400 years double ageInYears = ageInDays / daysInYear; //can be shifted ... not so precise 

La soluzione per 2 è quella che non è così precisa nel determinare l’età totale, ma è percepita come precisa dalle persone. Di solito le persone lo usano anche quando calcolano la loro età “manualmente”:

 DateTime birth = DateTime.Parse("1.1.2000"); DateTime today = DateTime.Today; int age = today.Year - birth.Year; //people perceive their age in years if (today.Month < birth.Month || ((today.Month == birth.Month) && (today.Day < birth.Day))) { age--; //birthday in current year not yet reached, we are 1 year younger ;) //+ no birthday for 29.2. guys ... sorry, just wrong date for birth } 

Note a 2 .:

  • Questa è la mia soluzione preferita
  • Non possiamo utilizzare DateTime.DayOfYear o TimeSpans, poiché spostano il numero di giorni negli anni bisestili
  • Ho messo un po 'più di linee per la leggibilità

Ancora una nota ... Creerei 2 metodi per sovraccarico statico, uno per l'uso universale, il secondo per l'utilizzo:

 public static int GetAge(DateTime bithDay, DateTime today) { //chosen solution method body } public static int GetAge(DateTime birthDay) { return GetAge(birthDay, DateTime.Now); } 

Molti anni fa, per fornire un espediente per il calcolo dell’età sul mio sito web, ho scritto una funzione per calcolare l’età in una frazione. Questa è una veloce porta di quella funzione in C # (dalla versione PHP ). Temo di non essere stato in grado di testare la versione C #, ma spero vi piaccia lo stesso!

(In effetti questo è un po ‘ingannevole allo scopo di mostrare i profili utente su Stack Overflow, ma forse i lettori troveranno qualche utilità per questo. :-))

 double AgeDiff(DateTime date1, DateTime date2) { double years = date2.Year - date1.Year; /* * If date2 and date1 + round(date2 - date1) are on different sides * of 29 February, then our partial year is considered to have 366 * days total, otherwise it's 365. Note that 59 is the day number * of 29 Feb. */ double fraction = 365 + (DateTime.IsLeapYear(date2.Year) && date2.DayOfYear >= 59 && (date1.DayOfYear < 59 || date1.DayOfYear > date2.DayOfYear) ? 1 : 0); /* * The only really nontrivial case is if date1 is in a leap year, * and date2 is not. So let's handle the others first. */ if (DateTime.IsLeapYear(date2.Year) == DateTime.IsLeapYear(date1.Year)) return years + (date2.DayOfYear - date1.DayOfYear) / fraction; /* * If date2 is in a leap year, but date1 is not and is March or * beyond, shift up by a day. */ if (DateTime.IsLeapYear(date2.Year)) { return years + (date2.DayOfYear - date1.DayOfYear - (date1.DayOfYear >= 59 ? 1 : 0)) / fraction; } /* * If date1 is not on 29 February, shift down date1 by a day if * March or later. Proceed normally. */ if (date1.DayOfYear != 59) { return years + (date2.DayOfYear - date1.DayOfYear + (date1.DayOfYear > 59 ? 1 : 0)) / fraction; } /* * Okay, here date1 is on 29 February, and date2 is not on a leap * year. What to do now? On 28 Feb in date2's year, the ``age'' * should be just shy of a whole number, and on 1 Mar should be * just over. Perhaps the easiest way is to a point halfway * between those two: 58.5. */ return years + (date2.DayOfYear - 58.5) / fraction; } 

Questa è la versione che usiamo qui. Funziona, ed è abbastanza semplice. È la stessa idea di Jeff, ma penso che sia un po ‘più chiaro perché separa la logica per la sottrazione di uno, quindi è un po’ più facile da capire.

 public static int GetAge(this DateTime dateOfBirth, DateTime dateAsAt) { return dateAsAt.Year - dateOfBirth.Year - (dateOfBirth.DayOfYear < dateAsAt.DayOfYear ? 0 : 1); } 

Potresti espandere l'operatore ternario per renderlo ancora più chiaro, se pensi che quel genere di cose non sia chiaro.

Ovviamente questo è fatto come metodo di estensione su DateTime , ma chiaramente puoi prendere quella riga di codice che fa il lavoro e metterlo ovunque. Qui abbiamo un altro sovraccarico del metodo Extension che passa in DateTime.Now , solo per completezza.

Io uso questo:

 public static class DateTimeExtensions { public static int Age(this DateTime birthDate) { return Age(birthDate, DateTime.Now); } public static int Age(this DateTime birthDate, DateTime offsetDate) { int result=0; result = offsetDate.Year - birthDate.Year; if (offsetDate.DayOfYear < birthDate.DayOfYear) { result--; } return result; } } 

Il miglior modo che io conosca a causa degli anni bisestili e tutto è:

 DateTime birthDate = new DateTime(2000,3,1); int age = (int)Math.Floor((DateTime.Now - birthDate).TotalDays / 365.25D); 

Spero che questo ti aiuti.

Questo dà “più dettagli” a questa domanda. Forse questo è quello che stai cercando

 DateTime birth = new DateTime(1974, 8, 29); DateTime today = DateTime.Now; TimeSpan span = today - birth; DateTime age = DateTime.MinValue + span; // Make adjustment due to MinValue equalling 1/1/1 int years = age.Year - 1; int months = age.Month - 1; int days = age.Day - 1; // Print out not only how many years old they are but give months and days as well Console.Write("{0} years, {1} months, {2} days", years, months, days); 

Ho creato una funzione definita dall’utente di SQL Server per calcolare l’età di qualcuno, data la loro data di nascita. Questo è utile quando ne hai bisogno come parte di una query:

 using System; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; public partial class UserDefinedFunctions { [SqlFunction(DataAccess = DataAccessKind.Read)] public static SqlInt32 CalculateAge(string strBirthDate) { DateTime dtBirthDate = new DateTime(); dtBirthDate = Convert.ToDateTime(strBirthDate); DateTime dtToday = DateTime.Now; // get the difference in years int years = dtToday.Year - dtBirthDate.Year; // subtract another year if we're before the // birth day in the current year if (dtToday.Month < dtBirthDate.Month || (dtToday.Month == dtBirthDate.Month && dtToday.Day < dtBirthDate.Day)) years=years-1; int intCustomerAge = years; return intCustomerAge; } }; 

Ho passato un po ‘di tempo a lavorare su questo e ne ho ricavato questo per calcolare l’età di qualcuno in anni, mesi e giorni. Ho provato contro il problema del 29 febbraio e gli anni bisestili e sembra funzionare, apprezzerei qualsiasi feedback:

 public void LoopAge(DateTime myDOB, DateTime FutureDate) { int years = 0; int months = 0; int days = 0; DateTime tmpMyDOB = new DateTime(myDOB.Year, myDOB.Month, 1); DateTime tmpFutureDate = new DateTime(FutureDate.Year, FutureDate.Month, 1); while (tmpMyDOB.AddYears(years).AddMonths(months) < tmpFutureDate) { months++; if (months > 12) { years++; months = months - 12; } } if (FutureDate.Day >= myDOB.Day) { days = days + FutureDate.Day - myDOB.Day; } else { months--; if (months < 0) { years--; months = months + 12; } days += DateTime.DaysInMonth( FutureDate.AddMonths(-1).Year, FutureDate.AddMonths(-1).Month ) + FutureDate.Day - myDOB.Day; } //add an extra day if the dob is a leap day if (DateTime.IsLeapYear(myDOB.Year) && myDOB.Month == 2 && myDOB.Day == 29) { //but only if the future date is less than 1st March if (FutureDate >= new DateTime(FutureDate.Year, 3, 1)) days++; } } 

Mantenerlo semplice (e possibilmente stupido :)).

 DateTime birth = new DateTime(1975, 09, 27, 01, 00, 00, 00); TimeSpan ts = DateTime.Now - birth; Console.WriteLine("You are approximately " + ts.TotalSeconds.ToString() + " seconds old."); 
 TimeSpan diff = DateTime.Now - birthdayDateTime; string age = String.Format("{0:%y} years, {0:%M} months, {0:%d}, days old", diff); 

Non sono sicuro di come ti piacerebbe che fosse restituito a te, quindi ho appena creato una stringa leggibile.

Dobbiamo considerare le persone che sono più piccole di 1 anno? come cultura cinese, descriviamo l’età dei bambini piccoli di 2 mesi o 4 settimane.

Di seguito è la mia implementazione, non è così semplice come quello che ho immaginato, soprattutto per gestire date come 2/28.

 public static string HowOld(DateTime birthday, DateTime now) { if (now < birthday) throw new ArgumentOutOfRangeException("birthday must be less than now."); TimeSpan diff = now - birthday; int diffDays = (int)diff.TotalDays; if (diffDays > 7)//year, month and week { int age = now.Year - birthday.Year; if (birthday > now.AddYears(-age)) age--; if (age > 0) { return age + (age > 1 ? " years" : " year"); } else {// month and week DateTime d = birthday; int diffMonth = 1; while (d.AddMonths(diffMonth) <= now) { diffMonth++; } age = diffMonth-1; if (age == 1 && d.Day > now.Day) age--; if (age > 0) { return age + (age > 1 ? " months" : " month"); } else { age = diffDays / 7; return age + (age > 1 ? " weeks" : " week"); } } } else if (diffDays > 0) { int age = diffDays; return age + (age > 1 ? " days" : " day"); } else { int age = diffDays; return "just born"; } } 

Questa implementazione è passata sotto i casi di test.

 [TestMethod] public void TestAge() { string age = HowOld(new DateTime(2011, 1, 1), new DateTime(2012, 11, 30)); Assert.AreEqual("1 year", age); age = HowOld(new DateTime(2011, 11, 30), new DateTime(2012, 11, 30)); Assert.AreEqual("1 year", age); age = HowOld(new DateTime(2001, 1, 1), new DateTime(2012, 11, 30)); Assert.AreEqual("11 years", age); age = HowOld(new DateTime(2012, 1, 1), new DateTime(2012, 11, 30)); Assert.AreEqual("10 months", age); age = HowOld(new DateTime(2011, 12, 1), new DateTime(2012, 11, 30)); Assert.AreEqual("11 months", age); age = HowOld(new DateTime(2012, 10, 1), new DateTime(2012, 11, 30)); Assert.AreEqual("1 month", age); age = HowOld(new DateTime(2008, 2, 28), new DateTime(2009, 2, 28)); Assert.AreEqual("1 year", age); age = HowOld(new DateTime(2008, 3, 28), new DateTime(2009, 2, 28)); Assert.AreEqual("11 months", age); age = HowOld(new DateTime(2008, 3, 28), new DateTime(2009, 3, 28)); Assert.AreEqual("1 year", age); age = HowOld(new DateTime(2009, 1, 28), new DateTime(2009, 2, 28)); Assert.AreEqual("1 month", age); age = HowOld(new DateTime(2009, 2, 1), new DateTime(2009, 3, 1)); Assert.AreEqual("1 month", age); // NOTE. // new DateTime(2008, 1, 31).AddMonths(1) == new DateTime(2009, 2, 28); // new DateTime(2008, 1, 28).AddMonths(1) == new DateTime(2009, 2, 28); age = HowOld(new DateTime(2009, 1, 31), new DateTime(2009, 2, 28)); Assert.AreEqual("4 weeks", age); age = HowOld(new DateTime(2009, 2, 1), new DateTime(2009, 2, 28)); Assert.AreEqual("3 weeks", age); age = HowOld(new DateTime(2009, 2, 1), new DateTime(2009, 3, 1)); Assert.AreEqual("1 month", age); age = HowOld(new DateTime(2012, 11, 5), new DateTime(2012, 11, 30)); Assert.AreEqual("3 weeks", age); age = HowOld(new DateTime(2012, 11, 1), new DateTime(2012, 11, 30)); Assert.AreEqual("4 weeks", age); age = HowOld(new DateTime(2012, 11, 20), new DateTime(2012, 11, 30)); Assert.AreEqual("1 week", age); age = HowOld(new DateTime(2012, 11, 25), new DateTime(2012, 11, 30)); Assert.AreEqual("5 days", age); age = HowOld(new DateTime(2012, 11, 29), new DateTime(2012, 11, 30)); Assert.AreEqual("1 day", age); age = HowOld(new DateTime(2012, 11, 30), new DateTime(2012, 11, 30)); Assert.AreEqual("just born", age); age = HowOld(new DateTime(2000, 2, 29), new DateTime(2009, 2, 28)); Assert.AreEqual("8 years", age); age = HowOld(new DateTime(2000, 2, 29), new DateTime(2009, 3, 1)); Assert.AreEqual("9 years", age); Exception e = null; try { age = HowOld(new DateTime(2012, 12, 1), new DateTime(2012, 11, 30)); } catch (ArgumentOutOfRangeException ex) { e = ex; } Assert.IsTrue(e != null); } 

Spero che sia utile

Ecco un’altra risposta:

 public static int AgeInYears(DateTime birthday, DateTime today) { return ((today.Year - birthday.Year) * 372 + (today.Month - birthday.Month) * 31 + (today.Day - birthday.Day)) / 372; } 

Questo è stato ampiamente testato unitamente. Sembra un po ‘”magico”. Il numero 372 è il numero di giorni che ci sarebbero in un anno se ogni mese avesse 31 giorni.

La spiegazione del perché funziona ( sollevato da qui ) è:

Yn = DateTime.Now.Year, Yb = birthday.Year, Mn = DateTime.Now.Month, Mb = birthday.Month, Dn = DateTime.Now.Day, Db = birthday.Day

age = Yn - Yb + (31*(Mn - Mb) + (Dn - Db)) / 372

Sappiamo che ciò di cui abbiamo bisogno è Yn-Yb se la data è già stata raggiunta, Yn-Yb-1 se non è già stata raggiunta.

a) Se Mn , abbiamo -341 <= 31*(Mn-Mb) <= -31 and -30 <= Dn-Db <= 30

-371 <= 31*(Mn - Mb) + (Dn - Db) <= -1

Con divisione intera

(31*(Mn - Mb) + (Dn - Db)) / 372 = -1

b) If Mn=Mb and Dn , we have 31*(Mn - Mb) = 0 and -30 <= Dn-Db <= -1

With integer division, again

(31*(Mn - Mb) + (Dn - Db)) / 372 = -1

c) If Mn>Mb , we have 31 <= 31*(Mn-Mb) <= 341 and -30 <= Dn-Db <= 30

1 <= 31*(Mn - Mb) + (Dn - Db) <= 371

With integer division

(31*(Mn - Mb) + (Dn - Db)) / 372 = 0

d) If Mn=Mb and Dn>Db , we have 31*(Mn - Mb) = 0 and 1 <= Dn-Db <= 3 0

With integer division, again

(31*(Mn - Mb) + (Dn - Db)) / 372 = 0

e) If Mn=Mb and Dn=Db , we have 31*(Mn - Mb) + Dn-Db = 0

and therefore (31*(Mn - Mb) + (Dn - Db)) / 372 = 0

The simplest way I’ve ever found is this. It works correctly for the US and western europe locales. Can’t speak to other locales, especially places like China. 4 extra compares, at most, following the initial computation of age.

 public int AgeInYears(DateTime birthDate, DateTime referenceDate) { Debug.Assert(referenceDate >= birthDate, "birth date must be on or prior to the reference date"); DateTime birth = birthDate.Date; DateTime reference = referenceDate.Date; int years = (reference.Year - birth.Year); // // an offset of -1 is applied if the birth date has // not yet occurred in the current year. // if (reference.Month > birth.Month); else if (reference.Month < birth.Month) --years; else // in birth month { if (reference.Day < birth.Day) --years; } return years ; } 

I was looking over the answers to this and noticed that nobody has made reference to regulatory/legal implications of leap day births. For instance, per Wikipedia , if you're born on February 29th in various jurisdictions, you're non-leap year birthday varies:

  • In the United Kingdom and Hong Kong: it's the ordinal day of the year, so the next day, March 1st is your birthday.
  • In New Zealand: it's the previous day, February 28th for the purposes of driver licencing, and March 1st for other purposes.
  • Taiwan: it's February 28th.

And as near as I can tell, in the US, the statutes are silent on the matter, leaving it up to the common law and to how various regulatory bodies define things in their regulations.

To that end, an improvement:

 public enum LeapDayRule { OrdinalDay = 1 , LastDayOfMonth = 2 , } static int ComputeAgeInYears(DateTime birth, DateTime reference, LeapYearBirthdayRule ruleInEffect) { bool isLeapYearBirthday = CultureInfo.CurrentCulture.Calendar.IsLeapDay(birth.Year, birth.Month, birth.Day); DateTime cutoff; if (isLeapYearBirthday && !DateTime.IsLeapYear(reference.Year)) { switch (ruleInEffect) { case LeapDayRule.OrdinalDay: cutoff = new DateTime(reference.Year, 1, 1) .AddDays(birth.DayOfYear - 1); break; case LeapDayRule.LastDayOfMonth: cutoff = new DateTime(reference.Year, birth.Month, 1) .AddMonths(1) .AddDays(-1); break; default: throw new InvalidOperationException(); } } else { cutoff = new DateTime(reference.Year, birth.Month, birth.Day); } int age = (reference.Year - birth.Year) + (reference >= cutoff ? 0 : -1); return age < 0 ? 0 : age; } 

It should be noted that this code assumes:

  • A western (European) reckoning of age, and
  • A calendar, like the Gregorian calendar that inserts a single leap day at the end of a month.

This is one of the most accurate answer that is able to resolve the birthday of 29th of Feb compare to any year of 28th Feb.

 public int GetAge(DateTime birthDate) { int age = DateTime.Now.Year - birthDate.Year; if (birthDate.DayOfYear > DateTime.Now.DayOfYear) age--; return age; } 

Here is a solution.

 DateTime dateOfBirth = new DateTime(2000, 4, 18); DateTime currentDate = DateTime.Now; int ageInYears = 0; int ageInMonths = 0; int ageInDays = 0; ageInDays = currentDate.Day - dateOfBirth.Day; ageInMonths = currentDate.Month - dateOfBirth.Month; ageInYears = currentDate.Year - dateOfBirth.Year; if (ageInDays < 0) { ageInDays += DateTime.DaysInMonth(currentDate.Year, currentDate.Month); ageInMonths = ageInMonths--; if (ageInMonths < 0) { ageInMonths += 12; ageInYears--; } } if (ageInMonths < 0) { ageInMonths += 12; ageInYears--; } Console.WriteLine("{0}, {1}, {2}", ageInYears, ageInMonths, ageInDays); 

This is not a direct answer, but more of a philosophical reasoning about the problem at hand from a quasi-scientific point of view.

I would argue that the question does not specify the unit nor culture in which to measure age, most answers seem to assume an integer annual representation. The SI-unit for time is second , ergo the correct generic answer should be (of course assuming normalized DateTime and taking no regard whatsoever to relativistic effects):

 var lifeInSeconds = (DateTime.Now.Ticks - then.Ticks)/TickFactor; 

In the Christian way of calculating age in years:

 var then = ... // Then, in this case the birthday var now = DateTime.UtcNow; int age = now.Year - then.Year; if (now.AddYears(-age) < then) age--; 

In finance there is a similar problem when calculating something often referred to as the Day Count Fraction , which roughly is a number of years for a given period. And the age issue is really a time measuring issue.

Example for the actual/actual (counting all days "correctly") convention:

 DateTime start, end = .... // Whatever, assume start is before end double startYearContribution = 1 - (double) start.DayOfYear / (double) (DateTime.IsLeapYear(start.Year) ? 366 : 365); double endYearContribution = (double)end.DayOfYear / (double)(DateTime.IsLeapYear(end.Year) ? 366 : 365); double middleContribution = (double) (end.Year - start.Year - 1); double DCF = startYearContribution + endYearContribution + middleContribution; 

Another quite common way to measure time generally is by "serializing" (the dude who named this date convention must seriously have been trippin'):

 DateTime start, end = .... // Whatever, assume start is before end int days = (end - start).Days; 

I wonder how long we have to go before a relativistic age in seconds becomes more useful than the rough approximation of earth-around-sun-cycles during one's lifetime so far 🙂 Or in other words, when a period must be given a location or a function representing motion for itself to be valid 🙂

How about this solution?

 static string CalcAge(DateTime birthDay) { DateTime currentDate = DateTime.Now; int approximateAge = currentDate.Year - birthDay.Year; int daysToNextBirthDay = (birthDay.Month * 30 + birthDay.Day) - (currentDate.Month * 30 + currentDate.Day) ; if (approximateAge == 0 || approximateAge == 1) { int month = Math.Abs(daysToNextBirthDay / 30); int days = Math.Abs(daysToNextBirthDay % 30); if (month == 0) return "Your age is: " + daysToNextBirthDay + " days"; return "Your age is: " + month + " months and " + days + " days"; ; } if (daysToNextBirthDay > 0) return "Your age is: " + --approximateAge + " Years"; return "Your age is: " + approximateAge + " Years"; ; } 

I have a customized method to calculate age, plus a bonus validation message just in case it helps:

 public void GetAge(DateTime dob, DateTime now, out int years, out int months, out int days) { years = 0; months = 0; days = 0; DateTime tmpdob = new DateTime(dob.Year, dob.Month, 1); DateTime tmpnow = new DateTime(now.Year, now.Month, 1); while (tmpdob.AddYears(years).AddMonths(months) < tmpnow) { months++; if (months > 12) { years++; months = months - 12; } } if (now.Day >= dob.Day) days = days + now.Day - dob.Day; else { months--; if (months < 0) { years--; months = months + 12; } days += DateTime.DaysInMonth(now.AddMonths(-1).Year, now.AddMonths(-1).Month) + now.Day - dob.Day; } if (DateTime.IsLeapYear(dob.Year) && dob.Month == 2 && dob.Day == 29 && now >= new DateTime(now.Year, 3, 1)) days++; } private string ValidateDate(DateTime dob) //This method will validate the date { int Years = 0; int Months = 0; int Days = 0; GetAge(dob, DateTime.Now, out Years, out Months, out Days); if (Years < 18) message = Years + " is too young. Please try again on your 18th birthday."; else if (Years >= 65) message = Years + " is too old. Date of Birth must not be 65 or older."; else return null; //Denotes validation passed } 

Method call here and pass out datetime value (MM/dd/yyyy if server set to USA locale). Replace this with anything a messagebox or any container to display:

 DateTime dob = DateTime.Parse("03/10/1982"); string message = ValidateDate(dob); lbldatemessage.Visible = !StringIsNullOrWhitespace(message); lbldatemessage.Text = message ?? ""; //Ternary if message is null then default to empty string 

Remember you can format the message any way you like.

 private int GetAge(int _year, int _month, int _day { DateTime yourBirthDate= new DateTime(_year, _month, _day); DateTime todaysDateTime = DateTime.Today; int noOfYears = todaysDateTime.Year - yourBirthDate.Year; if (DateTime.Now.Month < yourBirthDate.Month || (DateTime.Now.Month == yourBirthDate.Month && DateTime.Now.Day < yourBirthDate.Day)) { noOfYears--; } return noOfYears; } 

I used ScArcher2’s solution for an accurate Year calculation of a persons age but I needed to take it further and calculate their Months and Days along with the Years.

  public static Dictionary CurrentAgeInYearsMonthsDays(DateTime? ndtBirthDate, DateTime? ndtReferralDate) { //---------------------------------------------------------------------- // Can't determine age if we don't have a dates. //---------------------------------------------------------------------- if (ndtBirthDate == null) return null; if (ndtReferralDate == null) return null; DateTime dtBirthDate = Convert.ToDateTime(ndtBirthDate); DateTime dtReferralDate = Convert.ToDateTime(ndtReferralDate); //---------------------------------------------------------------------- // Create our Variables //---------------------------------------------------------------------- Dictionary dYMD = new Dictionary(); int iNowDate, iBirthDate, iYears, iMonths, iDays; string sDif = ""; //---------------------------------------------------------------------- // Store off current date/time and DOB into local variables //---------------------------------------------------------------------- iNowDate = int.Parse(dtReferralDate.ToString("yyyyMMdd")); iBirthDate = int.Parse(dtBirthDate.ToString("yyyyMMdd")); //---------------------------------------------------------------------- // Calculate Years //---------------------------------------------------------------------- sDif = (iNowDate - iBirthDate).ToString(); iYears = int.Parse(sDif.Substring(0, sDif.Length - 4)); //---------------------------------------------------------------------- // Store Years in Return Value //---------------------------------------------------------------------- dYMD.Add("Years", iYears); //---------------------------------------------------------------------- // Calculate Months //---------------------------------------------------------------------- if (dtBirthDate.Month > dtReferralDate.Month) iMonths = 12 - dtBirthDate.Month + dtReferralDate.Month - 1; else iMonths = dtBirthDate.Month - dtReferralDate.Month; //---------------------------------------------------------------------- // Store Months in Return Value //---------------------------------------------------------------------- dYMD.Add("Months", iMonths); //---------------------------------------------------------------------- // Calculate Remaining Days //---------------------------------------------------------------------- if (dtBirthDate.Day > dtReferralDate.Day) //Logic: Figure out the days in month previous to the current month, or the admitted month. // Subtract the birthday from the total days which will give us how many days the person has lived since their birthdate day the previous month. // then take the referral date and simply add the number of days the person has lived this month. //If referral date is january, we need to go back to the following year's December to get the days in that month. if (dtReferralDate.Month == 1) iDays = DateTime.DaysInMonth(dtReferralDate.Year - 1, 12) - dtBirthDate.Day + dtReferralDate.Day; else iDays = DateTime.DaysInMonth(dtReferralDate.Year, dtReferralDate.Month - 1) - dtBirthDate.Day + dtReferralDate.Day; else iDays = dtReferralDate.Day - dtBirthDate.Day; //---------------------------------------------------------------------- // Store Days in Return Value //---------------------------------------------------------------------- dYMD.Add("Days", iDays); return dYMD; } 

SQL version:

 declare @dd smalldatetime = '1980-04-01' declare @age int = YEAR(GETDATE())-YEAR(@dd) if (@dd> DATEADD(YYYY, [email protected], GETDATE())) set @age = @age -1 print @age 

I’ve made one small change to Mark Soen’s answer: I’ve rewriten the third line so that the expression can be parsed a bit more easily.

 public int AgeInYears(DateTime bday) { DateTime now = DateTime.Today; int age = now.Year - bday.Year; if (bday.AddYears(age) > now) age--; return age; } 

I’ve also made it into a function for the sake of clarity.