{"id":556,"date":"2016-12-16T13:08:37","date_gmt":"2016-12-16T12:08:37","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=556"},"modified":"2021-06-04T12:48:34","modified_gmt":"2021-06-04T11:48:34","slug":"c-birthdays","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-birthdays\/","title":{"rendered":"C# Birthdays"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static int DateTimeToInt(DateTime d) => (d.Year * 100 + d.Month) * 100 + d.Day;\npublic static double GetAge(DateTime dateOfBirth, DateTime onDate) => (DateTimeToInt(onDate) - DateTimeToInt(dateOfBirth)) \/ 10000;\npublic static double GetAge(DateTime dateOfBirth, DateTime? onDate) => GetAge(dateOfBirth, onDate ?? DateTime.Today);\n\nvoid Main()\n{\n\tConsole.WriteLine($\"Age: {GetAge(new DateTime(1987, 9, 10), DateTime.Today)}\");\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\n\nnamespace Test1\n{\n    class Program\n    {\n        public static IEnumerable&lt;KeyValuePair&lt;DateTime, T>> GetAnniversaries(IEnumerable&lt;IGrouping&lt;DateTime, T>> anniversaries, DateTime from, DateTime to)\n        {\n            List&lt;KeyValuePair&lt;DateTime, T>> result = new List&lt;KeyValuePair&lt;DateTime, T>>();\n            int year = from.Year;\n            for (year = from.Year; year &lt;= to.Year; year += 1) { \n                foreach (var anniversary in anniversaries) { \n                    var date = anniversary.Key; \n                    var anniversaryDay = new DateTime(year, date.Month, date.Day); \n                    if (anniversaryDay >= from.Date &amp;&amp; anniversaryDay &lt;= to.Date)\n                    {\n                        foreach (var item in anniversary)\n                        {\n                            result.Add(new KeyValuePair&lt;DateTime, T>(anniversaryDay, item));\n                        }\n                    }\n                }\n            }\n            result.Sort(delegate (KeyValuePair&lt;DateTime, T> a, KeyValuePair&lt;DateTime, T> b) {\n                return a.Key.CompareTo(b.Key);\n            });\n\n            return result;\n        }\n\n        public static IEnumerable&lt;KeyValuePair&lt;DateTime, T>> GetSpecialAnniversaries(IEnumerable&lt;IGrouping&lt;DateTime, T>> anniversaries, DateTime from, DateTime to)\n        {\n            List&lt;KeyValuePair&lt;DateTime, T>> result = new List&lt;KeyValuePair&lt;DateTime, T>>();\n            DateTime currentDate = from.Date;\n            while (currentDate &lt; to)\n            {\n                foreach (var anniversary in anniversaries)\n                {\n                    var date = anniversary.Key;\n                    DateTime start = new DateTime(date.Year, date.Month, date.Day);\n                    DateTime end = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day);\n\n                    double years = (end.Year - start.Year);\n                    double months = (end.Month - start.Month);\n                    int yearMonth = (int)((years + (months \/ 12.0)) * 10.0);\n                    var dayEquals = start.Day == end.Day;\n\n                    if (dayEquals &amp;&amp; (yearMonth == 125 || yearMonth == 250 || yearMonth == 400))\n                    {\n                        foreach (var item in anniversary)\n                        {\n                            result.Add(new KeyValuePair&lt;DateTime, T>(currentDate, item));\n                        }\n                    }\n                }\n                currentDate = currentDate.AddDays(1);\n            }\n            result.Sort(delegate (KeyValuePair&lt;DateTime, T> a, KeyValuePair&lt;DateTime, T> b) {\n                return a.Key.CompareTo(b.Key);\n            });\n\n            return result;\n        }\n\n        public class Anniversary\n        {\n            public string Name;\n            public DateTime Date;\n        }\n\n        public static double GetAge(DateTime anniversaryDate, DateTime? onDate = null, bool halfYear = false)\n        {\n            \/\/ Save today's date.\n            if (!onDate.HasValue)\n            {\n                onDate = DateTime.Today;\n            }\n\n            \/\/ Calculate the age.\n            double age = onDate.Value.Year - anniversaryDate.Year;\n\n            if (halfYear)\n            {\n                double months = (onDate.Value.Month - anniversaryDate.Month) \/ 12.0;\n                age += months;\n                age = (Math.Round(2 * age, MidpointRounding.AwayFromZero)) \/ 2;\n            }\n\n            \/\/ Do stuff with it.\n            if (anniversaryDate > onDate.Value.AddYears((int)-age)) age--;\n            return age;\n        }\n\n        public static void TestGetBirthDays()\n        {\n            var anniversaries = new Anniversary[] {\n                new Anniversary { Name = \"Niels\", Date = new DateTime(1987, 12, 12) },\n                new Anniversary { Name = \"Test\", Date = new DateTime(1987, 12, 12) },\n                new Anniversary { Name = \"Test\", Date = new DateTime(1987, 12, 13) },\n                new Anniversary { Name = \"Chris\", Date = new DateTime(1987, 1, 2) },\n            };\n\n            var anniversariesByDate = anniversaries.GroupBy(a => a.Date);\n            var currentAnniversaries = GetAnniversaries(anniversariesByDate, DateTime.Today, DateTime.Today.AddDays(300));\n\n            foreach (var anniversaryByDate in currentAnniversaries)\n            {\n                var anniversaryDate = anniversaryByDate.Key;\n                var date = anniversaryByDate.Value.Date;\n                var name = anniversaryByDate.Value.Name;\n                double age = GetAge(date, anniversaryDate);\n                \n                Console.WriteLine(String.Format(\"Date: {0}; Name: {1}; Age: {2}\", date, name, age));\n            }\n        }\n\n        static void Main(string[] args)\n        {\n            TestGetBirthDays();\n            Console.ReadKey();\n        }\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-556","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=556"}],"version-history":[{"count":9,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/556\/revisions"}],"predecessor-version":[{"id":5105,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/556\/revisions\/5105"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}