Today is very special day – in a way.. You see, today’s date is the same when read forwards or backwards – it’s a palindrome!

Well, at least if you live in a parts of the world with a date format such as day-month-year or year-month-day and you are using the Gregorian calendar. But that does cover a huge chunk of the world’s population, according to Wikipedia.

Allow me to demonstrate:

21. february 2012 = 21.02.2012 = 21022012

It turns out that today will be the last palindromic date in a while. The next one does not occur until 02.february 2020. It’s probably a sign, for sure.. 🙂

I created the little hack listed below for finding palindrome dates. It looks for palindromes both using the typical western date format, the North American format, and the ISO format for dates. The palindromic dates for the last 3 years and next 10 years are:

2010-01-02: 20100102
2010-02-01: 01022010
2011-02-11: 11022011
2011-11-02: 20111102
2012-02-21: 21022012
2020-02-02: 02022020
2020-02-02: 02022020
2021-02-12: 12022021
2021-12-02: 20211202

Here is the source:

/* 
 * Palindromic date finder hack 
 * By Lars Warholm, Norway
 * Copyright 2012 larslab.com
 * Released under GNU GPL v3.0
 */
using System;
using System.Collections.Generic;
using System.Linq;

namespace PalindromicDates
{
    public class PalindromDate
    {
        public DateTime Date { get; set; }

        public string PalindromicForm { get; set; }
    }


    public static class StringExts
    {
        public static string ReverseString(this string str)
        {
            char[] reverse = str.Reverse().ToArray();
            return new string(reverse);
        }
    }


    class PalindromicDateFinder
    {
        public DateTime StartDate { get; set; }

        public DateTime EndDate { get; set; }


        /// <summary>
        /// Look for palindromic dates in range StartDate .. EndDate
        /// </summary>
        public PalindromDate[] FindPalindromes()
        {
            List<PalindromDate> palindromes = new List<PalindromDate>();

            palindromes.AddRange(FindPalindromes("ddMMyyyy"));
            palindromes.AddRange(FindPalindromes("MMddyyyy"));
            palindromes.AddRange(FindPalindromes("yyyyMMdd"));

            palindromes.Sort((x, y) => { return x.Date.CompareTo(y.Date); });

            // remove duplicates
            for (int i = 0; i < palindromes.Count-1; i++)
            {
                if (palindromes[i].Date.CompareTo(palindromes[i + 1].Date) == 0)
                {
                    palindromes.RemoveAt(i + 1);
                }
            }

            return palindromes.ToArray();
        }


        /// <summary>
        /// Look for palindromic dates in range StartDate .. EndDate, using the specified date-to-string format
        /// </summary>
        private PalindromDate[] FindPalindromes(string dateFormat)
        {
            List<PalindromDate> result = new List<PalindromDate>();

            DateTime day = StartDate.Date;
            DateTime endDay = EndDate.Date;

            while (day.CompareTo(endDay) < 1)
            {
                string dayStr = day.ToString(dateFormat);

                if (dayStr == dayStr.ReverseString())
                {
                    result.Add(new PalindromDate() { Date = day, PalindromicForm = dayStr });
                }

                day = day.AddDays(1);
            }

            return result.ToArray();
        }



        static void Main(string[] args)
        {
            var p = new PalindromicDateFinder() { StartDate = DateTime.Now.Subtract(new TimeSpan(1100,0,0,0)), EndDate = DateTime.Now.AddDays(3650) };
            var palindromeDates = p.FindPalindromes();

            Console.WriteLine();

            foreach (var pDate in palindromeDates)
            {
                Console.WriteLine("{0:yyyy-MM-dd}: {1}", pDate.Date, pDate.PalindromicForm);
            }

            Console.WriteLine("Press Enter to quit");
            Console.ReadLine();
        }
    }
}
Advertisements