2020-02-02 Palindrome Day

Today is a palindrome day, i.e. the date written in the format YYYYmmdd can be read in reverse order to give the same result.

An article in a swiss newspaper reported, that this is the first palindrome day for 900 years, a little thinking about the problem easily will give you dates like 2001-10-02, 2010-01-02 or 2011-11-02 that are also palindrome days, but only for the format yyyyMMdd, different dates are palindrome dates for ddMMyyyy. I was curious to see how many palindrome days in either format exist from a given date (I used the year 1 AD (there is no year 0!!!), so I created this little script that prints out all the palindrome days since 01.01.01, days that are palindromes in both formats are highlighted:

List of PalindromeDates

And here is the script:

    <h2>List of PalindromeDates</h2>
    <div id="palindrome"></div>
    <script language="JavaScript">
        palindromeDates();

        function palindromeDates() {
            //var current = new Date("2020-01-30");   
            //const end = new Date("2020-02-03");
            var current = new Date("0001-01-01");
            const end = new Date("2100-01-01");
            var palindromeHtml = "ddmmYYYY, YYYYmmdd, Date<br/>";

            while (current < end) {
                isddmmYYYYPalindrome = checkPalindrome(getddmmYYYYString(current));
                isYYYYmmddPalindrome = checkPalindrome(getYYYYmmddString(current));
                if (isYYYYmmddPalindrome && isddmmYYYYPalindrome){
                    palindromeHtml = palindromeHtml + '<h3>';
                }
                if (isddmmYYYYPalindrome || isYYYYmmddPalindrome) {                    
                    if (isddmmYYYYPalindrome) {
                        palindromeHtml = palindromeHtml + getddmmYYYYString(current) + ', ';
                    } else {
                        palindromeHtml = palindromeHtml + '        , ';
                    }
                    if (isYYYYmmddPalindrome) {
                        palindromeHtml = palindromeHtml + getYYYYmmddString(current) + ', ';
                    } else {
                        palindromeHtml = palindromeHtml + '        , '
                    }
                    palindromeHtml = palindromeHtml + current.toISOString().slice(0, 10) + '<br/>'
                }
                current = new Date(current.getTime() + 86400000);
                if (isYYYYmmddPalindrome && isddmmYYYYPalindrome){
                    palindromeHtml = palindromeHtml + '</h3>';
                }
            }
            document.getElementById("palindrome").innerHTML = '<pre>' +palindromeHtml+ '</pre>';
        }

        function checkPalindrome(aString) {
            var isPalindrome = true;
            var chars = aString.split("");
            for (var i = 0; i < chars.length / 2; i++) {
                if (chars[i] != chars[(chars.length - 1 - i)]) {
                    isPalindrome = false;
                    break;
                }
            }
            return isPalindrome;
        }

        function getYYYYmmddString(aDate) {
            return aDate.toISOString().slice(0, 10).replace(/-/g, "");
        }

        function getddmmYYYYString(aDate) {
            var yearString = aDate.toISOString().slice(0, 4);
            var monthString = aDate.toISOString().slice(5, 7);
            var dayString = aDate.toISOString().slice(8, 10);
            return dateString = dayString + monthString + yearString;
        }
    </script>

Because we don’t have months with more than 31 days, there is a large gap between 1380-08-31 and 2001-10-02 but the article is easily proven wrong.