Skip to content

Instantly share code, notes, and snippets.

@karminski
Created December 12, 2018 11:14
Show Gist options
  • Save karminski/6f5868483de02b0660809f3a34811afa to your computer and use it in GitHub Desktop.
Save karminski/6f5868483de02b0660809f3a34811afa to your computer and use it in GitHub Desktop.
{
"answerList": [
{
"actionTime": "2018-07-05 03:51:43Z",
"answer": "<p>An easy to understand and simple solution.</p>\n\n<pre><code>// Save today's date.\nvar today = DateTime.Today;\n// Calculate the age.\nvar age = today.Year - birthdate.Year;\n// Go back to the year the person was born in case of a leap year\nif (birthdate &gt; today.AddYears(-age)) age--;\n</code></pre>\n\n<p>However, this assumes you are looking for the <em>western</em> idea of age and not using <a href=\"https://en.wikipedia.org/wiki/East_Asian_age_reckoning\" rel=\"noreferrer\"><em>East Asian reckoning</em></a>.</p>",
"isAccepted": true,
"vote": 1804
},
{
"actionTime": "2017-01-01 01:58:36Z",
"answer": "<p>This is a strange way to do it, but if you format the date to <code>yyyymmdd</code> and subtract the date of birth from the current date then drop the last 4 digits you've got the age :)</p>\n\n<p>I don't know C#, but I believe this will work in any language.</p>\n\n<pre><code>20080814 - 19800703 = 280111 \n</code></pre>\n\n<p>Drop the last 4 digits = <code>28</code>.</p>\n\n<p>C# Code:</p>\n\n<pre><code>int now = int.Parse(DateTime.Now.ToString(\"yyyyMMdd\"));\nint dob = int.Parse(dateOfBirth.ToString(\"yyyyMMdd\"));\nint age = (now - dob) / 10000;\n</code></pre>\n\n<p>Or alternatively without all the type conversion in the form of an extension method. Error checking omitted:</p>\n\n<pre><code>public static Int32 GetAge(this DateTime dateOfBirth)\n{\n var today = DateTime.Today;\n\n var a = (today.Year * 100 + today.Month) * 100 + today.Day;\n var b = (dateOfBirth.Year * 100 + dateOfBirth.Month) * 100 + dateOfBirth.Day;\n\n return (a - b) / 10000;\n}\n</code></pre>",
"isAccepted": false,
"vote": 922
},
{
"actionTime": "2016-02-07 00:04:29Z",
"answer": "<p>I don't know how the wrong solution can be accepted.\nThe correct C# snippet was written by Michael Stum</p>\n\n<p>Here is a test snippet:</p>\n\n<pre><code>DateTime bDay = new DateTime(2000, 2, 29);\nDateTime now = new DateTime(2009, 2, 28);\nMessageBox.Show(string.Format(\"Test {0} {1} {2}\",\n CalculateAgeWrong1(bDay, now), // outputs 9\n CalculateAgeWrong2(bDay, now), // outputs 9\n CalculateAgeCorrect(bDay, now))); // outputs 8\n</code></pre>\n\n<p>Here you have the methods:</p>\n\n<pre><code>public int CalculateAgeWrong1(DateTime birthDate, DateTime now)\n{\n return new DateTime(now.Subtract(birthDate).Ticks).Year - 1;\n}\n\npublic int CalculateAgeWrong2(DateTime birthDate, DateTime now)\n{\n int age = now.Year - birthDate.Year;\n\n if (now &lt; birthDate.AddYears(age))\n age--;\n\n return age;\n}\n\npublic int CalculateAgeCorrect(DateTime birthDate, DateTime now)\n{\n int age = now.Year - birthDate.Year;\n\n if (now.Month &lt; birthDate.Month || (now.Month == birthDate.Month &amp;&amp; now.Day &lt; birthDate.Day))\n age--;\n\n return age;\n}\n</code></pre>",
"isAccepted": false,
"vote": 353
},
{
"actionTime": "2018-07-05 03:52:31Z",
"answer": "<p>I don't think any of the answers so far provide for cultures that calculate age differently. See, for example, <a href=\"https://en.wikipedia.org/wiki/East_Asian_age_reckoning\" rel=\"noreferrer\">East Asian Age Reckoning</a> versus that in the West.</p>\n\n<p>Any <em>real</em> answer has to include localization. The <a href=\"https://en.wikipedia.org/wiki/Strategy_pattern\" rel=\"noreferrer\">Strategy Pattern</a> would probably be in order in this example.</p>",
"isAccepted": false,
"vote": 118
},
{
"actionTime": "2016-02-07 00:05:21Z",
"answer": "<p>The simple answer to this is to apply <code>AddYears</code> as shown below because this is the only native method to add years to the 29th of Feb. of leap years and obtain the correct result of the 28th of Feb. for common years. </p>\n\n<p>Some feel that 1th of Mar. is the birthday of leaplings but neither .Net nor any official rule supports this, nor does common logic explain why some born in February should have 75% of their birthdays in another month.</p>\n\n<p>Further, an Age method lends itself to be added as an extension to <code>DateTime</code>. By this you can obtain the age in the simplest possible way:</p>\n\n<ol>\n<li>List item</li>\n</ol>\n\n<p><strong>int age = birthDate.Age();</strong></p>\n\n<pre><code>public static class DateTimeExtensions\n{\n /// &lt;summary&gt;\n /// Calculates the age in years of the current System.DateTime object today.\n /// &lt;/summary&gt;\n /// &lt;param name=\"birthDate\"&gt;The date of birth&lt;/param&gt;\n /// &lt;returns&gt;Age in years today. 0 is returned for a future date of birth.&lt;/returns&gt;\n public static int Age(this DateTime birthDate)\n {\n return Age(birthDate, DateTime.Today);\n }\n\n /// &lt;summary&gt;\n /// Calculates the age in years of the current System.DateTime object on a later date.\n /// &lt;/summary&gt;\n /// &lt;param name=\"birthDate\"&gt;The date of birth&lt;/param&gt;\n /// &lt;param name=\"laterDate\"&gt;The date on which to calculate the age.&lt;/param&gt;\n /// &lt;returns&gt;Age in years on a later day. 0 is returned as minimum.&lt;/returns&gt;\n public static int Age(this DateTime birthDate, DateTime laterDate)\n {\n int age;\n age = laterDate.Year - birthDate.Year;\n\n if (age &gt; 0)\n {\n age -= Convert.ToInt32(laterDate.Date &lt; birthDate.Date.AddYears(age));\n }\n else\n {\n age = 0;\n }\n\n return age;\n }\n}\n</code></pre>\n\n<p>Now, run this test:</p>\n\n<pre><code>class Program\n{\n static void Main(string[] args)\n {\n RunTest();\n }\n\n private static void RunTest()\n {\n DateTime birthDate = new DateTime(2000, 2, 28);\n DateTime laterDate = new DateTime(2011, 2, 27);\n string iso = \"yyyy-MM-dd\";\n\n for (int i = 0; i &lt; 3; i++)\n {\n for (int j = 0; j &lt; 3; j++)\n {\n 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());\n }\n }\n\n Console.ReadKey();\n }\n}\n</code></pre>\n\n<p>The critical date example is this:</p>\n\n<p><strong>Birth date: 2000-02-29 Later date: 2011-02-28 Age: 11</strong></p>\n\n<p>Output:</p>\n\n<pre><code>{\n Birth date: 2000-02-28 Later date: 2011-02-27 Age: 10\n Birth date: 2000-02-28 Later date: 2011-02-28 Age: 11\n Birth date: 2000-02-28 Later date: 2011-03-01 Age: 11\n Birth date: 2000-02-29 Later date: 2011-02-27 Age: 10\n Birth date: 2000-02-29 Later date: 2011-02-28 Age: 11\n Birth date: 2000-02-29 Later date: 2011-03-01 Age: 11\n Birth date: 2000-03-01 Later date: 2011-02-27 Age: 10\n Birth date: 2000-03-01 Later date: 2011-02-28 Age: 10\n Birth date: 2000-03-01 Later date: 2011-03-01 Age: 11\n}\n</code></pre>\n\n<p>And for the later date 2012-02-28:</p>\n\n<pre><code>{\n Birth date: 2000-02-28 Later date: 2012-02-28 Age: 12\n Birth date: 2000-02-28 Later date: 2012-02-29 Age: 12\n Birth date: 2000-02-28 Later date: 2012-03-01 Age: 12\n Birth date: 2000-02-29 Later date: 2012-02-28 Age: 11\n Birth date: 2000-02-29 Later date: 2012-02-29 Age: 12\n Birth date: 2000-02-29 Later date: 2012-03-01 Age: 12\n Birth date: 2000-03-01 Later date: 2012-02-28 Age: 11\n Birth date: 2000-03-01 Later date: 2012-02-29 Age: 11\n Birth date: 2000-03-01 Later date: 2012-03-01 Age: 12\n}\n</code></pre>",
"isAccepted": false,
"vote": 100
},
{
"actionTime": "2013-08-01 13:20:56Z",
"answer": "<p>My suggestion</p>\n\n<pre><code>int age = (int) ((DateTime.Now - bday).TotalDays/365.242199);\n</code></pre>\n\n<p>That seems to have the year changing on the right date. (I spot tested up to age 107)</p>",
"isAccepted": false,
"vote": 78
},
{
"actionTime": "2017-07-25 17:11:03Z",
"answer": "<p>Another function, not by me but found on the web and refined it a bit:</p>\n\n<pre><code>public static int GetAge(DateTime birthDate)\n{\n DateTime n = DateTime.Now; // To avoid a race condition around midnight\n int age = n.Year - birthDate.Year;\n\n if (n.Month &lt; birthDate.Month || (n.Month == birthDate.Month &amp;&amp; n.Day &lt; birthDate.Day))\n age--;\n\n return age;\n}\n</code></pre>\n\n<p>Just two things that come into my mind: What about people from countries that do not use the gregorian calendar? DateTime.Now is in the server-specific culture i think. I have absolutely 0 knowledge about actually working with Asian calendars and I do not know if there is an easy way to convert dates between calendars, but just in case you're wondering about those chinese guys from the year 4660 :-)</p>",
"isAccepted": false,
"vote": 67
},
{
"actionTime": "2016-02-07 00:06:14Z",
"answer": "<p>2 Main problems to solve are:</p>\n\n<p><strong>1. Calculate Exact age</strong> - in years, months, days, etc.</p>\n\n<p><strong>2. Calculate Generally perceived age</strong> - people usually do not care how old they exactly are, they just care when their birthday in the current year is.</p>\n\n<hr>\n\n<p>Solution for <strong>1</strong> is obvious:</p>\n\n<pre><code>DateTime birth = DateTime.Parse(\"1.1.2000\");\nDateTime today = DateTime.Today; //we usually don't care about birth time\nTimeSpan age = today - birth; //.NET FCL should guarantee this as precise\ndouble ageInDays = age.TotalDays; //total number of days ... also precise\ndouble daysInYear = 365.2425; //statistical value for 400 years\ndouble ageInYears = ageInDays / daysInYear; //can be shifted ... not so precise\n</code></pre>\n\n<hr>\n\n<p>Solution for <strong>2</strong> is the one which is not so precise in determing total age, but is perceived as precise by people. People also usually use it, when they calculate their age \"manually\":</p>\n\n<pre><code>DateTime birth = DateTime.Parse(\"1.1.2000\");\nDateTime today = DateTime.Today;\nint age = today.Year - birth.Year; //people perceive their age in years\n\nif (today.Month &lt; birth.Month ||\n ((today.Month == birth.Month) &amp;&amp; (today.Day &lt; birth.Day)))\n{\n age--; //birthday in current year not yet reached, we are 1 year younger ;)\n //+ no birthday for 29.2. guys ... sorry, just wrong date for birth\n}\n</code></pre>\n\n<p>Notes to 2.:</p>\n\n<ul>\n<li>This is my preferred solution</li>\n<li>We cannot use DateTime.DayOfYear or TimeSpans, as they shift number of days in leap years</li>\n<li>I have put there little more lines for readability</li>\n</ul>\n\n<p>Just one more note ... I would create 2 static overloaded methods for it, one for universal usage, second for usage-friendliness:</p>\n\n<pre><code>public static int GetAge(DateTime bithDay, DateTime today) \n{ \n //chosen solution method body\n}\n\npublic static int GetAge(DateTime birthDay) \n{ \n return GetAge(birthDay, DateTime.Now);\n}\n</code></pre>",
"isAccepted": false,
"vote": 45
},
{
"actionTime": "2009-12-12 13:04:04Z",
"answer": "<p>I am late to the party, but here's a one-liner:</p>\n\n<pre><code>int age = new DateTime(DateTime.Now.Subtract(birthday).Ticks).Year-1;\n</code></pre>",
"isAccepted": false,
"vote": 44
},
{
"actionTime": "2010-05-23 10:19:08Z",
"answer": "<p>This is the version we use here. It works, and it's fairly simple. It's the same idea as Jeff's but I think it's a little clearer because it separates out the logic for subtracting one, so it's a little easier to understand.</p>\n\n<pre><code>public static int GetAge(this DateTime dateOfBirth, DateTime dateAsAt)\n{\n return dateAsAt.Year - dateOfBirth.Year - (dateOfBirth.DayOfYear &lt; dateAsAt.DayOfYear ? 0 : 1);\n}\n</code></pre>\n\n<p>You could expand the ternary operator to make it even clearer, if you think that sort of thing is unclear.</p>\n\n<p>Obviously this is done as an extension method on <code>DateTime</code>, but clearly you can grab that one line of code that does the work and put it anywhere. Here we have another overload of the Extension method that passes in <code>DateTime.Now</code>, just for completeness.</p>",
"isAccepted": false,
"vote": 33
},
{
"actionTime": "2015-06-03 11:10:46Z",
"answer": "<p>I use this:</p>\n\n<pre><code>public static class DateTimeExtensions\n{\n public static int Age(this DateTime birthDate)\n {\n return Age(birthDate, DateTime.Now);\n }\n\n public static int Age(this DateTime birthDate, DateTime offsetDate)\n {\n int result=0;\n result = offsetDate.Year - birthDate.Year;\n\n if (offsetDate.DayOfYear &lt; birthDate.DayOfYear)\n {\n result--;\n }\n\n return result;\n }\n}\n</code></pre>",
"isAccepted": false,
"vote": 30
},
{
"actionTime": "2008-08-01 15:26:37Z",
"answer": "<p>The best way that I know of because of leap years and everything is:</p>\n\n<pre><code>DateTime birthDate = new DateTime(2000,3,1);\nint age = (int)Math.Floor((DateTime.Now - birthDate).TotalDays / 365.25D);\n</code></pre>\n\n<p>Hope this helps.</p>",
"isAccepted": false,
"vote": 29
},
{
"actionTime": "2013-10-15 12:16:32Z",
"answer": "<p>This gives \"more detail\" to this question. Maybe this is what you're looking for</p>\n\n<pre><code>DateTime birth = new DateTime(1974, 8, 29);\nDateTime today = DateTime.Now;\nTimeSpan span = today - birth;\nDateTime age = DateTime.MinValue + span;\n\n// Make adjustment due to MinValue equalling 1/1/1\nint years = age.Year - 1;\nint months = age.Month - 1;\nint days = age.Day - 1;\n\n// Print out not only how many years old they are but give months and days as well\nConsole.Write(\"{0} years, {1} months, {2} days\", years, months, days);\n</code></pre>",
"isAccepted": false,
"vote": 27
},
{
"actionTime": "2016-02-07 00:07:00Z",
"answer": "<p>I have created a SQL Server User Defined Function to calculate someone's age, given their birthdate. This is useful when you need it as part of a query:</p>\n\n<pre><code>using System;\nusing System.Data;\nusing System.Data.Sql;\nusing System.Data.SqlClient;\nusing System.Data.SqlTypes;\nusing Microsoft.SqlServer.Server;\n\npublic partial class UserDefinedFunctions\n{\n [SqlFunction(DataAccess = DataAccessKind.Read)]\n public static SqlInt32 CalculateAge(string strBirthDate)\n {\n DateTime dtBirthDate = new DateTime();\n dtBirthDate = Convert.ToDateTime(strBirthDate);\n DateTime dtToday = DateTime.Now;\n\n // get the difference in years\n int years = dtToday.Year - dtBirthDate.Year;\n\n // subtract another year if we're before the\n // birth day in the current year\n if (dtToday.Month &lt; dtBirthDate.Month || (dtToday.Month == dtBirthDate.Month &amp;&amp; dtToday.Day &lt; dtBirthDate.Day))\n years=years-1;\n\n int intCustomerAge = years;\n return intCustomerAge;\n }\n};\n</code></pre>",
"isAccepted": false,
"vote": 23
},
{
"actionTime": "2016-02-07 00:07:23Z",
"answer": "<p>I've spent some time working on this and came up with this to calculate someone's age in years, months and days. I've tested against the Feb 29th problem and leap years and it seems to work, I'd appreciate any feedback:</p>\n\n<pre><code>public void LoopAge(DateTime myDOB, DateTime FutureDate)\n{\n int years = 0;\n int months = 0;\n int days = 0;\n\n DateTime tmpMyDOB = new DateTime(myDOB.Year, myDOB.Month, 1);\n\n DateTime tmpFutureDate = new DateTime(FutureDate.Year, FutureDate.Month, 1);\n\n while (tmpMyDOB.AddYears(years).AddMonths(months) &lt; tmpFutureDate)\n {\n months++;\n\n if (months &gt; 12)\n {\n years++;\n months = months - 12;\n }\n }\n\n if (FutureDate.Day &gt;= myDOB.Day)\n {\n days = days + FutureDate.Day - myDOB.Day;\n }\n else\n {\n months--;\n\n if (months &lt; 0)\n {\n years--;\n months = months + 12;\n }\n\n days +=\n DateTime.DaysInMonth(\n FutureDate.AddMonths(-1).Year, FutureDate.AddMonths(-1).Month\n ) + FutureDate.Day - myDOB.Day;\n\n }\n\n //add an extra day if the dob is a leap day\n if (DateTime.IsLeapYear(myDOB.Year) &amp;&amp; myDOB.Month == 2 &amp;&amp; myDOB.Day == 29)\n {\n //but only if the future date is less than 1st March\n if (FutureDate &gt;= new DateTime(FutureDate.Year, 3, 1))\n days++;\n }\n\n}\n</code></pre>",
"isAccepted": false,
"vote": 22
},
{
"actionTime": "2018-07-05 03:54:02Z",
"answer": "<p>Here's yet another answer:</p>\n\n<pre><code>public static int AgeInYears(DateTime birthday, DateTime today)\n{\n return ((today.Year - birthday.Year) * 372 + (today.Month - birthday.Month) * 31 + (today.Day - birthday.Day)) / 372;\n}\n</code></pre>\n\n<p>This has been extensively unit-tested. It does look a bit \"magic\". The number 372 is the number of days there would be in a year if every month had 31 days.</p>\n\n<p>The explanation of why it works (<a href=\"https://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/ba4a98af-aab3-4c59-bdee-611334e502f2\" rel=\"noreferrer\">lifted from here</a>) is:</p>\n\n<blockquote>\n <p>Let's set <code>Yn = DateTime.Now.Year, Yb = birthday.Year, Mn = DateTime.Now.Month, Mb = birthday.Month, Dn = DateTime.Now.Day, Db = birthday.Day</code></p>\n \n <p><code>age = Yn - Yb + (31*(Mn - Mb) + (Dn - Db)) / 372</code></p>\n \n <p>We know that what we need is either <code>Yn-Yb</code> if the date has already been reached, <code>Yn-Yb-1</code> if it has not.</p>\n \n <p>a) If <code>Mn&lt;Mb</code>, we have <code>-341 &lt;= 31*(Mn-Mb) &lt;= -31 and -30 &lt;= Dn-Db &lt;= 30</code></p>\n \n <p><code>-371 &lt;= 31*(Mn - Mb) + (Dn - Db) &lt;= -1</code></p>\n \n <p>With integer division</p>\n \n <p><code>(31*(Mn - Mb) + (Dn - Db)) / 372 = -1</code></p>\n \n <p>b) If <code>Mn=Mb</code> and <code>Dn&lt;Db</code>, we have <code>31*(Mn - Mb) = 0 and -30 &lt;= Dn-Db &lt;= -1</code></p>\n \n <p>With integer division, again</p>\n \n <p><code>(31*(Mn - Mb) + (Dn - Db)) / 372 = -1</code></p>\n \n <p>c) If <code>Mn&gt;Mb</code>, we have <code>31 &lt;= 31*(Mn-Mb) &lt;= 341 and -30 &lt;= Dn-Db &lt;= 30</code></p>\n \n <p><code>1 &lt;= 31*(Mn - Mb) + (Dn - Db) &lt;= 371</code></p>\n \n <p>With integer division</p>\n \n <p><code>(31*(Mn - Mb) + (Dn - Db)) / 372 = 0</code></p>\n \n <p>d) If <code>Mn=Mb</code> and <code>Dn&gt;Db</code>, we have <code>31*(Mn - Mb) = 0 and 1 &lt;= Dn-Db &lt;= 3</code>0</p>\n \n <p>With integer division, again</p>\n \n <p><code>(31*(Mn - Mb) + (Dn - Db)) / 372 = 0</code> </p>\n \n <p>e) If <code>Mn=Mb</code> and <code>Dn=Db</code>, we have <code>31*(Mn - Mb) + Dn-Db = 0</code></p>\n \n <p>and therefore <code>(31*(Mn - Mb) + (Dn - Db)) / 372 = 0</code></p>\n</blockquote>",
"isAccepted": false,
"vote": 19
},
{
"actionTime": "2016-02-07 00:18:38Z",
"answer": "<p>Do we need to consider people who is smaller than 1 year? as Chinese culture, we describe small babies' age as 2 months or 4 weeks. </p>\n\n<p>Below is my implementation, it is not as simple as what I imagined, especially to deal with date like 2/28. </p>\n\n<pre><code>public static string HowOld(DateTime birthday, DateTime now)\n{\n if (now &lt; birthday)\n throw new ArgumentOutOfRangeException(\"birthday must be less than now.\");\n\n TimeSpan diff = now - birthday;\n int diffDays = (int)diff.TotalDays;\n\n if (diffDays &gt; 7)//year, month and week\n {\n int age = now.Year - birthday.Year;\n\n if (birthday &gt; now.AddYears(-age))\n age--;\n\n if (age &gt; 0)\n {\n return age + (age &gt; 1 ? \" years\" : \" year\");\n }\n else\n {// month and week\n DateTime d = birthday;\n int diffMonth = 1;\n\n while (d.AddMonths(diffMonth) &lt;= now)\n {\n diffMonth++;\n }\n\n age = diffMonth-1;\n\n if (age == 1 &amp;&amp; d.Day &gt; now.Day)\n age--;\n\n if (age &gt; 0)\n {\n return age + (age &gt; 1 ? \" months\" : \" month\");\n }\n else\n {\n age = diffDays / 7;\n return age + (age &gt; 1 ? \" weeks\" : \" week\");\n }\n }\n }\n else if (diffDays &gt; 0)\n {\n int age = diffDays;\n return age + (age &gt; 1 ? \" days\" : \" day\");\n }\n else\n {\n int age = diffDays;\n return \"just born\";\n }\n}\n</code></pre>\n\n<p>This implementation has passed below test cases.</p>\n\n<pre><code>[TestMethod]\npublic void TestAge()\n{\n string age = HowOld(new DateTime(2011, 1, 1), new DateTime(2012, 11, 30));\n Assert.AreEqual(\"1 year\", age);\n\n age = HowOld(new DateTime(2011, 11, 30), new DateTime(2012, 11, 30));\n Assert.AreEqual(\"1 year\", age);\n\n age = HowOld(new DateTime(2001, 1, 1), new DateTime(2012, 11, 30));\n Assert.AreEqual(\"11 years\", age);\n\n age = HowOld(new DateTime(2012, 1, 1), new DateTime(2012, 11, 30));\n Assert.AreEqual(\"10 months\", age);\n\n age = HowOld(new DateTime(2011, 12, 1), new DateTime(2012, 11, 30));\n Assert.AreEqual(\"11 months\", age);\n\n age = HowOld(new DateTime(2012, 10, 1), new DateTime(2012, 11, 30));\n Assert.AreEqual(\"1 month\", age);\n\n age = HowOld(new DateTime(2008, 2, 28), new DateTime(2009, 2, 28));\n Assert.AreEqual(\"1 year\", age);\n\n age = HowOld(new DateTime(2008, 3, 28), new DateTime(2009, 2, 28));\n Assert.AreEqual(\"11 months\", age);\n\n age = HowOld(new DateTime(2008, 3, 28), new DateTime(2009, 3, 28));\n Assert.AreEqual(\"1 year\", age);\n\n age = HowOld(new DateTime(2009, 1, 28), new DateTime(2009, 2, 28));\n Assert.AreEqual(\"1 month\", age);\n\n age = HowOld(new DateTime(2009, 2, 1), new DateTime(2009, 3, 1));\n Assert.AreEqual(\"1 month\", age);\n\n // NOTE.\n // new DateTime(2008, 1, 31).AddMonths(1) == new DateTime(2009, 2, 28);\n // new DateTime(2008, 1, 28).AddMonths(1) == new DateTime(2009, 2, 28);\n age = HowOld(new DateTime(2009, 1, 31), new DateTime(2009, 2, 28));\n Assert.AreEqual(\"4 weeks\", age);\n\n age = HowOld(new DateTime(2009, 2, 1), new DateTime(2009, 2, 28));\n Assert.AreEqual(\"3 weeks\", age);\n\n age = HowOld(new DateTime(2009, 2, 1), new DateTime(2009, 3, 1));\n Assert.AreEqual(\"1 month\", age);\n\n age = HowOld(new DateTime(2012, 11, 5), new DateTime(2012, 11, 30));\n Assert.AreEqual(\"3 weeks\", age);\n\n age = HowOld(new DateTime(2012, 11, 1), new DateTime(2012, 11, 30));\n Assert.AreEqual(\"4 weeks\", age);\n\n age = HowOld(new DateTime(2012, 11, 20), new DateTime(2012, 11, 30));\n Assert.AreEqual(\"1 week\", age);\n\n age = HowOld(new DateTime(2012, 11, 25), new DateTime(2012, 11, 30));\n Assert.AreEqual(\"5 days\", age);\n\n age = HowOld(new DateTime(2012, 11, 29), new DateTime(2012, 11, 30));\n Assert.AreEqual(\"1 day\", age);\n\n age = HowOld(new DateTime(2012, 11, 30), new DateTime(2012, 11, 30));\n Assert.AreEqual(\"just born\", age);\n\n age = HowOld(new DateTime(2000, 2, 29), new DateTime(2009, 2, 28));\n Assert.AreEqual(\"8 years\", age);\n\n age = HowOld(new DateTime(2000, 2, 29), new DateTime(2009, 3, 1));\n Assert.AreEqual(\"9 years\", age);\n\n Exception e = null;\n\n try\n {\n age = HowOld(new DateTime(2012, 12, 1), new DateTime(2012, 11, 30));\n }\n catch (ArgumentOutOfRangeException ex)\n {\n e = ex;\n }\n\n Assert.IsTrue(e != null);\n}\n</code></pre>\n\n<p>Hope it's helpful.</p>",
"isAccepted": false,
"vote": 18
},
{
"actionTime": "2010-08-18 14:29:10Z",
"answer": "<p>Keeping it simple (and possibly stupid:)).</p>\n\n<pre><code>DateTime birth = new DateTime(1975, 09, 27, 01, 00, 00, 00);\nTimeSpan ts = DateTime.Now - birth;\nConsole.WriteLine(\"You are approximately \" + ts.TotalSeconds.ToString() + \" seconds old.\");\n</code></pre>",
"isAccepted": false,
"vote": 17
},
{
"actionTime": "2013-09-19 15:18:53Z",
"answer": "<pre><code>TimeSpan diff = DateTime.Now - birthdayDateTime;\nstring age = String.Format(\"{0:%y} years, {0:%M} months, {0:%d}, days old\", diff);\n</code></pre>\n\n<p>I'm not sure how exactly you'd like it returned to you, so I just made a readable string.</p>",
"isAccepted": false,
"vote": 17
},
{
"actionTime": "2018-07-05 03:53:35Z",
"answer": "<p>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.</p>\n\n<pre><code>public int AgeInYears(DateTime birthDate, DateTime referenceDate)\n{\n Debug.Assert(referenceDate &gt;= birthDate, \n \"birth date must be on or prior to the reference date\");\n\n DateTime birth = birthDate.Date;\n DateTime reference = referenceDate.Date;\n int years = (reference.Year - birth.Year);\n\n //\n // an offset of -1 is applied if the birth date has \n // not yet occurred in the current year.\n //\n if (reference.Month &gt; birth.Month);\n else if (reference.Month &lt; birth.Month) \n --years;\n else // in birth month\n {\n if (reference.Day &lt; birth.Day)\n --years;\n }\n\n return years ;\n}\n</code></pre>\n\n<p>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, <a href=\"https://en.wikipedia.org/wiki/February_29#Births\" rel=\"nofollow noreferrer\">per Wikipedia</a>, if you're born on February 29th in various jurisdictions, you're non-leap year birthday varies:</p>\n\n<ul>\n<li>In the United Kingdom and Hong Kong: it's the ordinal day of the year, so the next day, March 1st is your birthday.</li>\n<li>In New Zealand: it's the previous day, February 28th for the purposes of driver licencing, and March 1st for other purposes.</li>\n<li>Taiwan: it's February 28th.</li>\n</ul>\n\n<p>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.</p>\n\n<p>To that end, an improvement:</p>\n\n<pre><code>public enum LeapDayRule\n{\n OrdinalDay = 1 ,\n LastDayOfMonth = 2 ,\n}\n\nstatic int ComputeAgeInYears(DateTime birth, DateTime reference, LeapYearBirthdayRule ruleInEffect)\n{\n bool isLeapYearBirthday = CultureInfo.CurrentCulture.Calendar.IsLeapDay(birth.Year, birth.Month, birth.Day);\n DateTime cutoff;\n\n if (isLeapYearBirthday &amp;&amp; !DateTime.IsLeapYear(reference.Year))\n {\n switch (ruleInEffect)\n {\n case LeapDayRule.OrdinalDay:\n cutoff = new DateTime(reference.Year, 1, 1)\n .AddDays(birth.DayOfYear - 1);\n break;\n\n case LeapDayRule.LastDayOfMonth:\n cutoff = new DateTime(reference.Year, birth.Month, 1)\n .AddMonths(1)\n .AddDays(-1);\n break;\n\n default:\n throw new InvalidOperationException();\n }\n }\n else\n {\n cutoff = new DateTime(reference.Year, birth.Month, birth.Day);\n }\n\n int age = (reference.Year - birth.Year) + (reference &gt;= cutoff ? 0 : -1);\n return age &lt; 0 ? 0 : age;\n}\n</code></pre>\n\n<p>It should be noted that this code assumes:</p>\n\n<ul>\n<li>A western (European) reckoning of age, and</li>\n<li>A calendar, like the Gregorian calendar that inserts a single leap day at the end of a month.</li>\n</ul>",
"isAccepted": false,
"vote": 16
},
{
"actionTime": "2016-02-07 00:07:42Z",
"answer": "<p>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.</p>\n\n<pre><code>public int GetAge(DateTime birthDate)\n{\n int age = DateTime.Now.Year - birthDate.Year;\n\n if (birthDate.DayOfYear &gt; DateTime.Now.DayOfYear)\n age--;\n\n return age;\n}\n</code></pre>",
"isAccepted": false,
"vote": 15
},
{
"actionTime": "2018-02-22 16:36:45Z",
"answer": "<p>Here is a solution.</p>\n\n<pre><code>DateTime dateOfBirth = new DateTime(2000, 4, 18);\nDateTime currentDate = DateTime.Now;\n\nint ageInYears = 0;\nint ageInMonths = 0;\nint ageInDays = 0;\n\nageInDays = currentDate.Day - dateOfBirth.Day;\nageInMonths = currentDate.Month - dateOfBirth.Month;\nageInYears = currentDate.Year - dateOfBirth.Year;\n\nif (ageInDays &lt; 0)\n{\n ageInDays += DateTime.DaysInMonth(currentDate.Year, currentDate.Month);\n ageInMonths = ageInMonths--;\n\n if (ageInMonths &lt; 0)\n {\n ageInMonths += 12;\n ageInYears--;\n }\n}\n\nif (ageInMonths &lt; 0)\n{\n ageInMonths += 12;\n ageInYears--;\n}\n\nConsole.WriteLine(\"{0}, {1}, {2}\", ageInYears, ageInMonths, ageInDays);\n</code></pre>",
"isAccepted": false,
"vote": 15
},
{
"actionTime": "2017-10-31 11:32:39Z",
"answer": "<p>This is not a direct answer, but more of a philosophical reasoning about the problem at hand from a quasi-scientific point of view.</p>\n\n<p>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 <code>second</code>, ergo the correct generic answer should be (of course assuming normalized <code>DateTime</code> and taking no regard whatsoever to relativistic effects):</p>\n\n<pre><code>var lifeInSeconds = (DateTime.Now.Ticks - then.Ticks)/TickFactor;\n</code></pre>\n\n<p>In the Christian way of calculating age in years:</p>\n\n<pre><code>var then = ... // Then, in this case the birthday\nvar now = DateTime.UtcNow;\nint age = now.Year - then.Year;\nif (now.AddYears(-age) &lt; then) age--;\n</code></pre>\n\n<p>In finance there is a similar problem when calculating something often referred to as the <em>Day Count Fraction</em>, which roughly is a number of years for a given period. And the age issue is really a time measuring issue.</p>\n\n<p>Example for the actual/actual (counting all days \"correctly\") convention:</p>\n\n<pre><code>DateTime start, end = .... // Whatever, assume start is before end\n\ndouble startYearContribution = 1 - (double) start.DayOfYear / (double) (DateTime.IsLeapYear(start.Year) ? 366 : 365);\ndouble endYearContribution = (double)end.DayOfYear / (double)(DateTime.IsLeapYear(end.Year) ? 366 : 365);\ndouble middleContribution = (double) (end.Year - start.Year - 1);\n\ndouble DCF = startYearContribution + endYearContribution + middleContribution;\n</code></pre>\n\n<p>Another quite common way to measure time generally is by \"serializing\" (the dude who named this date convention must seriously have been trippin'):</p>\n\n<pre><code>DateTime start, end = .... // Whatever, assume start is before end\nint days = (end - start).Days;\n</code></pre>\n\n<p>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 :)</p>",
"isAccepted": false,
"vote": 14
},
{
"actionTime": "2016-02-07 00:14:20Z",
"answer": "<p>How about this solution? </p>\n\n<pre><code>static string CalcAge(DateTime birthDay)\n{\n DateTime currentDate = DateTime.Now; \n int approximateAge = currentDate.Year - birthDay.Year;\n int daysToNextBirthDay = (birthDay.Month * 30 + birthDay.Day) - \n (currentDate.Month * 30 + currentDate.Day) ;\n\n if (approximateAge == 0 || approximateAge == 1)\n { \n int month = Math.Abs(daysToNextBirthDay / 30);\n int days = Math.Abs(daysToNextBirthDay % 30);\n\n if (month == 0)\n return \"Your age is: \" + daysToNextBirthDay + \" days\";\n\n return \"Your age is: \" + month + \" months and \" + days + \" days\"; ;\n }\n\n if (daysToNextBirthDay &gt; 0)\n return \"Your age is: \" + --approximateAge + \" Years\";\n\n return \"Your age is: \" + approximateAge + \" Years\"; ;\n}\n</code></pre>",
"isAccepted": false,
"vote": 13
},
{
"actionTime": "2018-01-02 09:45:44Z",
"answer": "<p>I have a customized method to calculate age, plus a bonus validation message just in case it helps:</p>\n\n<pre><code>public void GetAge(DateTime dob, DateTime now, out int years, out int months, out int days)\n{\n years = 0;\n months = 0;\n days = 0;\n\n DateTime tmpdob = new DateTime(dob.Year, dob.Month, 1);\n DateTime tmpnow = new DateTime(now.Year, now.Month, 1);\n\n while (tmpdob.AddYears(years).AddMonths(months) &lt; tmpnow)\n {\n months++;\n if (months &gt; 12)\n {\n years++;\n months = months - 12;\n }\n }\n\n if (now.Day &gt;= dob.Day)\n days = days + now.Day - dob.Day;\n else\n {\n months--;\n if (months &lt; 0)\n {\n years--;\n months = months + 12;\n }\n days += DateTime.DaysInMonth(now.AddMonths(-1).Year, now.AddMonths(-1).Month) + now.Day - dob.Day;\n }\n\n if (DateTime.IsLeapYear(dob.Year) &amp;&amp; dob.Month == 2 &amp;&amp; dob.Day == 29 &amp;&amp; now &gt;= new DateTime(now.Year, 3, 1))\n days++;\n\n} \n\nprivate string ValidateDate(DateTime dob) //This method will validate the date\n{\n int Years = 0; int Months = 0; int Days = 0;\n\n GetAge(dob, DateTime.Now, out Years, out Months, out Days);\n\n if (Years &lt; 18)\n message = Years + \" is too young. Please try again on your 18th birthday.\";\n else if (Years &gt;= 65)\n message = Years + \" is too old. Date of Birth must not be 65 or older.\";\n else\n return null; //Denotes validation passed\n}\n</code></pre>\n\n<p>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:</p>\n\n<pre><code>DateTime dob = DateTime.Parse(\"03/10/1982\"); \n\nstring message = ValidateDate(dob);\n\nlbldatemessage.Visible = !StringIsNullOrWhitespace(message);\nlbldatemessage.Text = message ?? \"\"; //Ternary if message is null then default to empty string\n</code></pre>\n\n<p>Remember you can format the message any way you like.</p>",
"isAccepted": false,
"vote": 13
},
{
"actionTime": "2016-02-07 00:13:55Z",
"answer": "<pre><code>private int GetAge(int _year, int _month, int _day\n{\n DateTime yourBirthDate= new DateTime(_year, _month, _day);\n\n DateTime todaysDateTime = DateTime.Today;\n int noOfYears = todaysDateTime.Year - yourBirthDate.Year;\n\n if (DateTime.Now.Month &lt; yourBirthDate.Month ||\n (DateTime.Now.Month == yourBirthDate.Month &amp;&amp; DateTime.Now.Day &lt; yourBirthDate.Day))\n {\n noOfYears--;\n }\n\n return noOfYears;\n}\n</code></pre>",
"isAccepted": false,
"vote": 11
},
{
"actionTime": "2012-10-14 12:05:16Z",
"answer": "<p>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.</p>\n\n<pre><code> public static Dictionary&lt;string,int&gt; CurrentAgeInYearsMonthsDays(DateTime? ndtBirthDate, DateTime? ndtReferralDate)\n {\n //----------------------------------------------------------------------\n // Can't determine age if we don't have a dates.\n //----------------------------------------------------------------------\n if (ndtBirthDate == null) return null;\n if (ndtReferralDate == null) return null;\n\n DateTime dtBirthDate = Convert.ToDateTime(ndtBirthDate);\n DateTime dtReferralDate = Convert.ToDateTime(ndtReferralDate);\n\n //----------------------------------------------------------------------\n // Create our Variables\n //----------------------------------------------------------------------\n Dictionary&lt;string, int&gt; dYMD = new Dictionary&lt;string,int&gt;();\n int iNowDate, iBirthDate, iYears, iMonths, iDays;\n string sDif = \"\";\n\n //----------------------------------------------------------------------\n // Store off current date/time and DOB into local variables\n //---------------------------------------------------------------------- \n iNowDate = int.Parse(dtReferralDate.ToString(\"yyyyMMdd\"));\n iBirthDate = int.Parse(dtBirthDate.ToString(\"yyyyMMdd\"));\n\n //----------------------------------------------------------------------\n // Calculate Years\n //----------------------------------------------------------------------\n sDif = (iNowDate - iBirthDate).ToString();\n iYears = int.Parse(sDif.Substring(0, sDif.Length - 4));\n\n //----------------------------------------------------------------------\n // Store Years in Return Value\n //----------------------------------------------------------------------\n dYMD.Add(\"Years\", iYears);\n\n //----------------------------------------------------------------------\n // Calculate Months\n //----------------------------------------------------------------------\n if (dtBirthDate.Month &gt; dtReferralDate.Month)\n iMonths = 12 - dtBirthDate.Month + dtReferralDate.Month - 1;\n else\n iMonths = dtBirthDate.Month - dtReferralDate.Month;\n\n //----------------------------------------------------------------------\n // Store Months in Return Value\n //----------------------------------------------------------------------\n dYMD.Add(\"Months\", iMonths);\n\n //----------------------------------------------------------------------\n // Calculate Remaining Days\n //----------------------------------------------------------------------\n if (dtBirthDate.Day &gt; dtReferralDate.Day)\n //Logic: Figure out the days in month previous to the current month, or the admitted month.\n // 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.\n // then take the referral date and simply add the number of days the person has lived this month.\n\n //If referral date is january, we need to go back to the following year's December to get the days in that month.\n if (dtReferralDate.Month == 1)\n iDays = DateTime.DaysInMonth(dtReferralDate.Year - 1, 12) - dtBirthDate.Day + dtReferralDate.Day; \n else\n iDays = DateTime.DaysInMonth(dtReferralDate.Year, dtReferralDate.Month - 1) - dtBirthDate.Day + dtReferralDate.Day; \n else\n iDays = dtReferralDate.Day - dtBirthDate.Day; \n\n //----------------------------------------------------------------------\n // Store Days in Return Value\n //----------------------------------------------------------------------\n dYMD.Add(\"Days\", iDays);\n\n return dYMD;\n}\n</code></pre>",
"isAccepted": false,
"vote": 8
},
{
"actionTime": "2016-06-30 11:24:56Z",
"answer": "<p>SQL version:</p>\n\n<pre><code>declare @dd smalldatetime = '1980-04-01'\ndeclare @age int = YEAR(GETDATE())-YEAR(@dd)\nif (@dd&gt; DATEADD(YYYY, -@age, GETDATE())) set @age = @age -1\n\nprint @age \n</code></pre>",
"isAccepted": false,
"vote": 8
},
{
"actionTime": "2018-02-22 16:38:57Z",
"answer": "<p>I've made one small change to <a href=\"https://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c/1404#1404\">Mark Soen's</a> answer: I've rewriten the third line so that the expression can be parsed a bit more easily.</p>\n\n<pre><code>public int AgeInYears(DateTime bday)\n{\n DateTime now = DateTime.Today;\n int age = now.Year - bday.Year; \n if (bday.AddYears(age) &gt; now) \n age--;\n return age;\n}\n</code></pre>\n\n<p>I've also made it into a function for the sake of clarity.</p>",
"isAccepted": false,
"vote": 7
},
{
"actionTime": "2018-07-05 03:54:55Z",
"answer": "<p>The following approach (extract from <a href=\"https://www.codeproject.com/Articles/168662/Time-Period-Library-for-NET\" rel=\"nofollow noreferrer\">Time Period Library for .NET</a> class <em>DateDiff</em>) considers the calendar of the culture info:</p>\n\n<pre><code>// ----------------------------------------------------------------------\nprivate static int YearDiff( DateTime date1, DateTime date2 )\n{\n return YearDiff( date1, date2, DateTimeFormatInfo.CurrentInfo.Calendar );\n} // YearDiff\n\n// ----------------------------------------------------------------------\nprivate static int YearDiff( DateTime date1, DateTime date2, Calendar calendar )\n{\n if ( date1.Equals( date2 ) )\n {\n return 0;\n }\n\n int year1 = calendar.GetYear( date1 );\n int month1 = calendar.GetMonth( date1 );\n int year2 = calendar.GetYear( date2 );\n int month2 = calendar.GetMonth( date2 );\n\n // find the the day to compare\n int compareDay = date2.Day;\n int compareDaysPerMonth = calendar.GetDaysInMonth( year1, month1 );\n if ( compareDay &gt; compareDaysPerMonth )\n {\n compareDay = compareDaysPerMonth;\n }\n\n // build the compare date\n DateTime compareDate = new DateTime( year1, month2, compareDay,\n date2.Hour, date2.Minute, date2.Second, date2.Millisecond );\n if ( date2 &gt; date1 )\n {\n if ( compareDate &lt; date1 )\n {\n compareDate = compareDate.AddYears( 1 );\n }\n }\n else\n {\n if ( compareDate &gt; date1 )\n {\n compareDate = compareDate.AddYears( -1 );\n }\n }\n return year2 - calendar.GetYear( compareDate );\n} // YearDiff\n</code></pre>\n\n<p>Usage:</p>\n\n<pre><code>// ----------------------------------------------------------------------\npublic void CalculateAgeSamples()\n{\n PrintAge( new DateTime( 2000, 02, 29 ), new DateTime( 2009, 02, 28 ) );\n // &gt; Birthdate=29.02.2000, Age at 28.02.2009 is 8 years\n PrintAge( new DateTime( 2000, 02, 29 ), new DateTime( 2012, 02, 28 ) );\n // &gt; Birthdate=29.02.2000, Age at 28.02.2012 is 11 years\n} // CalculateAgeSamples\n\n// ----------------------------------------------------------------------\npublic void PrintAge( DateTime birthDate, DateTime moment )\n{\n Console.WriteLine( \"Birthdate={0:d}, Age at {1:d} is {2} years\", birthDate, moment, YearDiff( birthDate, moment ) );\n} // PrintAge\n</code></pre>",
"isAccepted": false,
"vote": 7
}
],
"linked": [
{
"id": 4127363,
"link": "/questions/4127363/date-difference-in-years-using-c-sharp?noredirect=1",
"title": "Date difference in years using C#",
"vote": 60
},
{
"id": 3152977,
"link": "/questions/3152977/calculate-the-difference-between-two-dates-and-get-the-value-in-years?noredirect=1",
"title": "Calculate the difference between two dates and get the value in years?",
"vote": 39
},
{
"id": 2194999,
"link": "/questions/2194999/how-to-calculate-an-age-based-on-a-birthday?noredirect=1",
"title": "How to calculate an age based on a birthday?",
"vote": 17
},
{
"id": 14766086,
"link": "/questions/14766086/how-to-calculate-age-in-years-from-dob-in-c-sharp?noredirect=1",
"title": "How to calculate age in years from dob in c#",
"vote": 8
},
{
"id": 673476,
"link": "/questions/673476/age-in-years-from-datetime-date-of-birth?noredirect=1",
"title": "Age in years from DateTime (Date of birth)",
"vote": 6
},
{
"id": 26504044,
"link": "/questions/26504044/simple-age-calculator-base-on-date-of-birth-in-c-sharp?noredirect=1",
"title": "Simple Age Calculator base on date of birth in C#",
"vote": 4
},
{
"id": 2237587,
"link": "/questions/2237587/how-can-i-calculate-age-by-datetimepicker?noredirect=1",
"title": "how can i calculate age by datetimepicker",
"vote": 2
},
{
"id": 3889845,
"link": "/questions/3889845/what-is-the-simplest-and-correct-way-to-calculate-age?noredirect=1",
"title": "what is the simplest and correct way to calculate age?",
"vote": 1
},
{
"id": 14476817,
"link": "/questions/14476817/how-to-accurately-get-difference-between-two-datetime-object-in-years-closed?noredirect=1",
"title": "How to accurately get difference between two DateTime object in &ldquo;Years&rdquo; [Closed, use NodaTime]",
"vote": -3
},
{
"id": 2902401,
"link": "/questions/2902401/calculating-age-based-on-date-of-birth-in-sql-and-c?noredirect=1",
"title": "Calculating age based on date of birth in SQL and C#?",
"vote": 1
}
],
"question": "How do I calculate someone&#39;s age in C#?",
"questionDescription": "Given a <code>DateTime</code> representing a person's birthday, how do I calculate their age in years? ",
"questionFavoritecount": 409,
"questionPostDate": "2008-07-31 23:40:59Z",
"questionPostViewed": 502228,
"questionTags": [
".net",
"c#",
"datetime"
],
"questionVote": 1743,
"related": [
{
"id": 11,
"link": "/questions/11/calculate-relative-time-in-c-sharp",
"title": "Calculate relative time in C#",
"vote": 1383
},
{
"id": 7074,
"link": "/questions/7074/what-is-the-difference-between-string-and-string-in-c",
"title": "What is the difference between String and string in C#?",
"vote": 5628
},
{
"id": 29482,
"link": "/questions/29482/cast-int-to-enum-in-c-sharp",
"title": "Cast int to enum in C#",
"vote": 2696
},
{
"id": 105372,
"link": "/questions/105372/how-do-i-enumerate-an-enum-in-c",
"title": "How do I enumerate an enum in C#?",
"vote": 3270
},
{
"id": 151005,
"link": "/questions/151005/how-to-create-excel-xls-and-xlsx-file-in-c-sharp-without-installing-ms-offic",
"title": "How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?",
"vote": 1640
},
{
"id": 221294,
"link": "/questions/221294/how-do-you-get-a-timestamp-in-javascript",
"title": "How do you get a timestamp in JavaScript?",
"vote": 3501
},
{
"id": 247621,
"link": "/questions/247621/what-are-the-correct-version-numbers-for-c",
"title": "What are the correct version numbers for C#?",
"vote": 2189
},
{
"id": 325933,
"link": "/questions/325933/determine-whether-two-date-ranges-overlap",
"title": "Determine Whether Two Date Ranges Overlap",
"vote": 1027
},
{
"id": 371328,
"link": "/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overridden",
"title": "Why is it important to override GetHashCode when Equals method is overridden?",
"vote": 1234
},
{
"id": 472906,
"link": "/questions/472906/how-do-i-get-a-consistent-byte-representation-of-strings-in-c-sharp-without-manu",
"title": "How do I get a consistent byte representation of strings in C# without manually specifying an encoding?",
"vote": 1978
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment