C# Birthdays

Date: 2016-12-16
public static int DateTimeToInt(DateTime d) => (d.Year * 100 + d.Month) * 100 + d.Day;
public static double GetAge(DateTime dateOfBirth, DateTime onDate) => (DateTimeToInt(onDate) - DateTimeToInt(dateOfBirth)) / 10000;
public static double GetAge(DateTime dateOfBirth, DateTime? onDate) => GetAge(dateOfBirth, onDate ?? DateTime.Today);

void Main()
{
	Console.WriteLine($"Age: {GetAge(new DateTime(1987, 9, 10), DateTime.Today)}");
}
using System;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Test1
{
    class Program
    {
        public static IEnumerable<KeyValuePair<DateTime, T>> GetAnniversaries(IEnumerable<IGrouping<DateTime, T>> anniversaries, DateTime from, DateTime to)
        {
            List<KeyValuePair<DateTime, T>> result = new List<KeyValuePair<DateTime, T>>();
            int year = from.Year;
            for (year = from.Year; year <= to.Year; year += 1) { 
                foreach (var anniversary in anniversaries) { 
                    var date = anniversary.Key; 
                    var anniversaryDay = new DateTime(year, date.Month, date.Day); 
                    if (anniversaryDay >= from.Date && anniversaryDay <= to.Date)
                    {
                        foreach (var item in anniversary)
                        {
                            result.Add(new KeyValuePair<DateTime, T>(anniversaryDay, item));
                        }
                    }
                }
            }
            result.Sort(delegate (KeyValuePair<DateTime, T> a, KeyValuePair<DateTime, T> b) {
                return a.Key.CompareTo(b.Key);
            });

            return result;
        }

        public static IEnumerable<KeyValuePair<DateTime, T>> GetSpecialAnniversaries(IEnumerable<IGrouping<DateTime, T>> anniversaries, DateTime from, DateTime to)
        {
            List<KeyValuePair<DateTime, T>> result = new List<KeyValuePair<DateTime, T>>();
            DateTime currentDate = from.Date;
            while (currentDate < to)
            {
                foreach (var anniversary in anniversaries)
                {
                    var date = anniversary.Key;
                    DateTime start = new DateTime(date.Year, date.Month, date.Day);
                    DateTime end = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day);

                    double years = (end.Year - start.Year);
                    double months = (end.Month - start.Month);
                    int yearMonth = (int)((years + (months / 12.0)) * 10.0);
                    var dayEquals = start.Day == end.Day;

                    if (dayEquals && (yearMonth == 125 || yearMonth == 250 || yearMonth == 400))
                    {
                        foreach (var item in anniversary)
                        {
                            result.Add(new KeyValuePair<DateTime, T>(currentDate, item));
                        }
                    }
                }
                currentDate = currentDate.AddDays(1);
            }
            result.Sort(delegate (KeyValuePair<DateTime, T> a, KeyValuePair<DateTime, T> b) {
                return a.Key.CompareTo(b.Key);
            });

            return result;
        }

        public class Anniversary
        {
            public string Name;
            public DateTime Date;
        }

        public static double GetAge(DateTime anniversaryDate, DateTime? onDate = null, bool halfYear = false)
        {
            // Save today's date.
            if (!onDate.HasValue)
            {
                onDate = DateTime.Today;
            }

            // Calculate the age.
            double age = onDate.Value.Year - anniversaryDate.Year;

            if (halfYear)
            {
                double months = (onDate.Value.Month - anniversaryDate.Month) / 12.0;
                age += months;
                age = (Math.Round(2 * age, MidpointRounding.AwayFromZero)) / 2;
            }

            // Do stuff with it.
            if (anniversaryDate > onDate.Value.AddYears((int)-age)) age--;
            return age;
        }

        public static void TestGetBirthDays()
        {
            var anniversaries = new Anniversary[] {
                new Anniversary { Name = "Niels", Date = new DateTime(1987, 12, 12) },
                new Anniversary { Name = "Test", Date = new DateTime(1987, 12, 12) },
                new Anniversary { Name = "Test", Date = new DateTime(1987, 12, 13) },
                new Anniversary { Name = "Chris", Date = new DateTime(1987, 1, 2) },
            };

            var anniversariesByDate = anniversaries.GroupBy(a => a.Date);
            var currentAnniversaries = GetAnniversaries(anniversariesByDate, DateTime.Today, DateTime.Today.AddDays(300));

            foreach (var anniversaryByDate in currentAnniversaries)
            {
                var anniversaryDate = anniversaryByDate.Key;
                var date = anniversaryByDate.Value.Date;
                var name = anniversaryByDate.Value.Name;
                double age = GetAge(date, anniversaryDate);
                
                Console.WriteLine(String.Format("Date: {0}; Name: {1}; Age: {2}", date, name, age));
            }
        }

        static void Main(string[] args)
        {
            TestGetBirthDays();
            Console.ReadKey();
        }
    }
}
5560cookie-checkC# Birthdays