Palindrome dates

From Rosetta Code
Task
Palindrome dates
You are encouraged to solve this task according to the task description, using any language you may know.

Today (2020-02-02) happens to be a palindrome, without the hyphens, not only for those countries which express their dates in the yyyy-mm-dd format but, unusually, also for countries which use the dd-mm-yyyy format.

Task

Write a program which calculates and shows the next 15 palindromic dates for those countries which express their dates in the yyyy-mm-dd format.

AppleScript[edit]

on palindromeDates(startYear, targetNumber)
script o
property output : {}
end script
 
set counter to 0
set y to startYear
repeat until ((counter = targetNumber) or (y > 9999))
-- Derive a month number from the last two digits of the current year number. It's valid if it's in the range 1 to 12.
set m to y mod 10 * 10 + y mod 100 div 10
if ((m > 0) and (m < 13)) then
-- Derive a day number from the first two digits of the year number.
set d to y div 100 mod 10 * 10 + y div 1000
-- It's valid if it's between 1 and 28. Otherwise, if it's between 29 and 31, check that it fits the month and year.
-- In fact though, it'll only ever be 2 or 12 in the period containing the 15 palindromic dates after 2020.
if ((d > 0) and ¬
((d < 29) ¬
or ((d < 31) and ((m is not 2) or ((d is 29) and (y mod 4 is 0) and ((y mod 100 > 0) or (y mod 400 is 0))))) ¬
or ((d is 31) and (m is not in {2, 4, 9, 6, 11})))) then
-- If the figures represent a valid date, add a yyyy-mm-dd format text to the end of the output list.
tell ((100000000 + y * 10000 + m * 100 + d) as text) to ¬
set end of o's output to text 2 thru 5 & ("-" & text 6 thru 7) & ("-" & text 8 thru 9)
set counter to counter + 1
end if
end if
set y to y + 1
end repeat
 
return o's output
end palindromeDates
 
palindromeDates(2021, 15)
Output:
{"2021-12-02", "2030-03-02", "2040-04-02", "2050-05-02", "2060-06-02", "2070-07-02", "2080-08-02", "2090-09-02", "2101-10-12", "2110-01-12", "2111-11-12", "2120-02-12", "2121-12-12", "2130-03-12", "2140-04-12"}

AWK[edit]

 
# syntax: GAWK -f PALINDROME_DATES.AWK
BEGIN {
show = 15
year_b = 2020
year_e = 9999
split("31,28,31,30,31,30,31,31,30,31,30,31",daynum_array,",") # days per month in non leap year
for (y=year_b; y<=year_e; y++) {
daynum_array[2] = (y % 400 == 0 || (y % 4 == 0 && y % 100)) ? 29 : 28
for (m=1; m<=12; m++) {
for (d=1; d<=daynum_array[m]; d++) {
ymd = sprintf("%04d%02d%02d",y,m,d)
if (substr(ymd,1,1) == substr(ymd,8,1)) { # speed up
if (ymd == reverse(ymd)) {
arr[++n] = ymd
}
}
}
}
}
printf("%04d0101-%04d1231=%d years, %d palindromes, showing first and last %d\n",year_b,year_e,year_e-year_b+1,n,show)
printf("YYYYMMDD YYYYMMDD\n")
for (i=1; i<=show; i++) {
printf("%s %s\n",arr[i],arr[n-show+i])
}
exit(0)
}
function reverse(str, i,rts) {
for (i=length(str); i>=1; i--) {
rts = rts substr(str,i,1)
}
return(rts)
}
 
Output:
20200101-99991231=7980 years, 285 palindromes, showing first and last 15
YYYYMMDD YYYYMMDD
20200202 91700719
20211202 91800819
20300302 91900919
20400402 92011029
20500502 92100129
20600602 92111129
20700702 92200229
20800802 92211229
20900902 92300329
21011012 92400429
21100112 92500529
21111112 92600629
21200212 92700729
21211212 92800829
21300312 92900929

C#[edit]

using System;
using System.Linq;
using System.Collections.Generic;
 
public class Program
{
static void Main()
{
foreach (var date in PalindromicDates(2021).Take(15)) WriteLine(date.ToString("yyyy-MM-dd"));
}
 
public static IEnumerable<DateTime> PalindromicDates(int startYear) {
for (int y = startYear; ; y++) {
int m = Reverse(y % 100);
int d = Reverse(y / 100);
if (IsValidDate(y, m, d, out var date)) yield return date;
}
 
int Reverse(int x) => x % 10 * 10 + x / 10;
bool IsValidDate(int y, int m, int d, out DateTime date) => DateTime.TryParse($"{y}-{m}-{d}", out date);
}
}
Output:
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12
2140-04-12

F#[edit]

// palindrome_dates.fsx
open System
 
let is_palindrome_date =
let date_string (date: DateTime) = date.ToString "yyyyMMdd"
let is_palindrome s =
let rev_string = Seq.rev >> Seq.map string >> String.concat ""
s = rev_string s
date_string >> is_palindrome
 
let palindrome_dates =
let rec loop date =
seq {
if is_palindrome_date date
then
yield date
yield! loop (date.AddDays 1.0)
else
yield! loop (date.AddDays 1.0)
}
loop DateTime.Now
 
let print_date =
let iso_string (date: DateTime) = date.ToString "yyyy-MM-dd"
iso_string >> printfn "%s"
 
palindrome_dates
|> Seq.take 15
|> Seq.iter print_date
 
Output:
> dotnet fsi palindrome_dates.fsx 
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12
2140-04-12

Factor[edit]

Brute force[edit]

A simple brute force solution that repeatedly increments a timestamp's day by one and checks whether it's a palindrome:

Works with: Factor version 0.99 2020-01-23
USING: calendar calendar.format io kernel lists lists.lazy
sequences sets ;
 
: palindrome-dates ( -- list )
2020 2 2 <date> [ 1 days time+ ] lfrom-by
[ timestamp>ymd ] lmap-lazy
[ "-" without dup reverse = ] lfilter ;
 
15 palindrome-dates ltake [ print ] leach
Output:
2020-02-02
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12

Faster[edit]

A faster version that directly generates palindromic numbers such as 20200202 and keeps those which are valid dates:

Works with: Factor version 0.99 2020-01-23
USING: calendar calendar.format continuations io kernel lists
lists.lazy math math.functions math.parser math.ranges sequences ;
 
: create-palindrome ( n odd? -- m )
dupd [ 10 /i ] when swap [ over 0 > ]
[ 10 * [ 10 /mod ] [ + ] bi* ] while nip ;
 
: palindromes ( -- list )
3 lfrom [
10 swap ^ dup 10 * [a,b)
[ [ t create-palindrome ] map ]
[ [ f create-palindrome ] map ] bi
[ sequence>list ] [email protected] lappend
] lmap-lazy lconcat [ 20200202 >= ] lfilter ;
 
: palindrome-dates ( -- list )
palindromes [
number>string 4 cut* 2 cut [ string>number ] [email protected]
[ <date> ] [ 4drop f ] recover
] lmap-lazy [ f = not ] lfilter ;
 
"10,000th palindrome date after 2020-02-02: " write
10,000 palindrome-dates lnth timestamp>ymd print
Output:
10,000th palindrome date after 2020-02-02: 1250101-05-21

Go[edit]

Simple brute force as speed is not an issue here.

package main
 
import (
"fmt"
"time"
)
 
func reverse(s string) string {
chars := []rune(s)
for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
chars[i], chars[j] = chars[j], chars[i]
}
return string(chars)
}
 
func main() {
const (
layout = "20060102"
layout2 = "2006-01-02"
)
fmt.Println("The next 15 palindromic dates in yyyymmdd format after 20200202 are:")
date := time.Date(2020, 2, 2, 0, 0, 0, 0, time.UTC)
count := 0
for count < 15 {
date = date.AddDate(0, 0, 1)
s := date.Format(layout)
r := reverse(s)
if r == s {
fmt.Println(date.Format(layout2))
count++
}
}
}
Output:
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12
2140-04-12

Or, a more ambitious version.

package main
 
import (
"fmt"
"sort"
"time"
)
 
func reverse(s string) string {
chars := []rune(s)
for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
chars[i], chars[j] = chars[j], chars[i]
}
return string(chars)
}
 
func findIndex(sl []string, s string) int {
return sort.Search(len(sl), func(i int) bool {
return sl[i] > s
})
}
 
func main() {
const (
layout = "20060102"
layout2 = "2006-01-02"
)
palins := []string{}
for i := 0; i < 10000; i++ {
y := fmt.Sprintf("%04d", i)
r := reverse(y)
if r[:2] > "12" || r[2:] > "31" {
continue
}
d := fmt.Sprintf("%s%s", y, r)
t, err := time.Parse(layout, d)
if err == nil {
palins = append(palins, t.Format(layout2))
}
}
le := len(palins)
i1 := findIndex(palins, "1001-01-01")
i2 := findIndex(palins, "2020-02-02")
fmt.Printf("There are %d palindromic dates after 0000-01-01 of which:\n", le)
fmt.Printf("  %d are after 1000-01-01\n", le-i1)
fmt.Printf("  %d are after 2020-02-02\n", le-i2)
fmt.Println("\nThe first 15 after 2020-02-02 are:")
for i := 0; i < 15; i++ {
if i != 0 && i%5 == 0 {
fmt.Println()
}
fmt.Printf("%s ", palins[i+i2])
}
fmt.Println("\n\nThe last 15 before 9999-12-31 are:")
for i := 15; i >= 1; i-- {
if i != 15 && i%5 == 0 {
fmt.Println()
}
fmt.Printf("%s ", palins[le-i])
}
fmt.Println()
}
Output:
There are 366 palindromic dates after 0000-01-01 of which:
          331 are after 1000-01-01
          284 are after 2020-02-02

The first 15 after 2020-02-02 are:
2021-12-02   2030-03-02   2040-04-02   2050-05-02   2060-06-02   
2070-07-02   2080-08-02   2090-09-02   2101-10-12   2110-01-12   
2111-11-12   2120-02-12   2121-12-12   2130-03-12   2140-04-12   

The last 15 before 9999-12-31 are:
9170-07-19   9180-08-19   9190-09-19   9201-10-29   9210-01-29   
9211-11-29   9220-02-29   9221-12-29   9230-03-29   9240-04-29   
9250-05-29   9260-06-29   9270-07-29   9280-08-29   9290-09-29  

Haskell[edit]

import Data.Time.Calendar (Day, fromGregorianValid)
import Data.List.Split (chunksOf)
import Data.List (unfoldr)
import Data.Tuple (swap)
import Data.Bool (bool)
import Data.Maybe (mapMaybe)
 
palinDates :: [Day]
palinDates = mapMaybe palinDay [2021 .. 9999]
 
palinDay :: Integer -> Maybe Day
palinDay y = fromGregorianValid y m d
where
[m, d] = unDigits <$> chunksOf 2 (reversedDecimalDigits (fromInteger y))
 
reversedDecimalDigits :: Int -> [Int]
reversedDecimalDigits =
unfoldr ((flip bool Nothing . Just . swap . flip quotRem 10) <*> (0 ==))
 
unDigits :: [Int] -> Int
unDigits = foldl ((+) . (10 *)) 0
 
main :: IO ()
main = do
let n = length palinDates
putStrLn $ "Count of palindromic dates [2021..9999]: " ++ show n
putStrLn "\nFirst 15:"
mapM_ print $ take 15 palinDates
putStrLn "\nLast 15:"
mapM_ print $ take 15 (drop (n - 15) palinDates)
Output:
Count of palindromic dates [2021..9999]: 284

First 15:
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12
2140-04-12

Last 15:
9170-07-19
9180-08-19
9190-09-19
9201-10-29
9210-01-29
9211-11-29
9220-02-29
9221-12-29
9230-03-29
9240-04-29
9250-05-29
9260-06-29
9270-07-29
9280-08-29
9290-09-29

Java[edit]

 
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
 
public class PalindromeDates {
 
public static void main(String[] args) {
LocalDate date = LocalDate.of(2020, 2, 3);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
DateTimeFormatter formatterDash = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.printf("First 15 palindrome dates after 2020-02-02 are:%n");
for ( int count = 0 ; count < 15 ; date = date.plusDays(1) ) {
String dateFormatted = date.format(formatter);
if ( dateFormatted.compareTo(new StringBuilder(dateFormatted).reverse().toString()) == 0 ) {
count++;
System.out.printf("date = %s%n", date.format(formatterDash));
}
}
}
 
}
 
Output:
First 15 palindrome dates after 2020-02-02 are:
date = 2021-12-02
date = 2030-03-02
date = 2040-04-02
date = 2050-05-02
date = 2060-06-02
date = 2070-07-02
date = 2080-08-02
date = 2090-09-02
date = 2101-10-12
date = 2110-01-12
date = 2111-11-12
date = 2120-02-12
date = 2121-12-12
date = 2130-03-12
date = 2140-04-12

Julia[edit]

Uses the built-in Dates package to check date validity but not for iteration.

using Dates
 
function datepalindromes(nextcount=20)
println("Date palindromes:")
count, d = 0, Date(1000, 1, 1)
for year in 2021:9200
try
dig = digits(year)
month = 10 * dig[1] + dig[2]
day = 10 * dig[3] + dig[4]
d = Date(year, month, day)
catch
continue
end
println(d)
count += 1
if count >= nextcount
break
end
end
end
 
datepalindromes()
 
Output:
Date palindromes:
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12
2140-04-12
2150-05-12
2160-06-12
2170-07-12
2180-08-12
2190-09-12

Perl[edit]

Date calculation[edit]

The more robust solution, using a date/time module.

use Time::Piece;
my $d = Time::Piece->strptime("2020-02-02", "%Y-%m-%d");
 
for (my $k = 1 ; $k <= 15 ; $d += Time::Piece::ONE_DAY) {
my $s = $d->strftime("%Y%m%d");
if ($s eq reverse($s) and ++$k) {
print $d->strftime("%Y-%m-%d\n");
}
}
Output:
2020-02-02
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12

String manipulation[edit]

Given the limited look-ahead required by the task, processing date-like strings can also work.

Library: ntheory
use strict;
use warnings;
use feature 'say';
use ntheory qw/forsetproduct/;
 
my $start = '2020-02-02' =~ s/-//gr;
my($y) = substr($start,0,4);
 
my(@dates,$cnt);
forsetproduct { push @dates, "@_" } [$y..$y+999],['01'..'12'],['01'..'31'];
for (@dates) {
(my $date = $_) =~ s/ //g;
next unless $date > $start and $date eq reverse $date;
say s/ /-/gr;
last if 15 == ++$cnt;
}
Output:
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12
2140-04-12

Phix[edit]

Note that parse_date_string() copes with 1/2/4 digit years, but (reasonably enough) throws a wobbly given 5-digit years and beyond.

include builtins\timedate.e 
sequence res = {}
for d=2021 to 9999 do
string s = sprintf("%4d",d),
t = reverse(s)
s &= "-"&t[1..2]&"-"&t[3..4]
sequence td = parse_date_string(s, {"YYYY-MM-DD"})
if timedate(td) then res = append(res,s) end if
end for
printf(1,"Count of palindromic dates [2021..9999]: %d\n\n",length(res))
printf(1,"first 15:\n%s\n",join_by(res[1..15],3,5))
printf(1,"last 15:\n%s\n",join_by(res[-15..-1],3,5))
Output:
Count of palindromic dates [2021..9999]: 284

first 15:
2021-12-02   2050-05-02   2080-08-02   2110-01-12   2121-12-12
2030-03-02   2060-06-02   2090-09-02   2111-11-12   2130-03-12
2040-04-02   2070-07-02   2101-10-12   2120-02-12   2140-04-12

last 15:
9170-07-19   9201-10-29   9220-02-29   9240-04-29   9270-07-29
9180-08-19   9210-01-29   9221-12-29   9250-05-29   9280-08-29
9190-09-19   9211-11-29   9230-03-29   9260-06-29   9290-09-29

Python[edit]

Functional[edit]

Defined in terms of string reversal:

Works with: Python version 3.7
'''Palindrome dates'''
 
from datetime import datetime
from itertools import chain
 
 
# palinDay :: Int -> [ISO Date]
def palinDay(y):
'''A possibly empty list containing the palindromic
date for the given year, if such a date exists.
'''

s = str(y)
r = s[::-1]
iso = '-'.join([s, r[0:2], r[2:]])
try:
datetime.strptime(iso, '%Y-%m-%d')
return [iso]
except ValueError:
return []
 
 
# --------------------------TEST---------------------------
# main :: IO ()
def main():
'''Count and samples of palindromic dates [2021..9999]
'''

palinDates = list(chain.from_iterable(
map(palinDay, range(2021, 10000))
))
for x in [
'Count of palindromic dates [2021..9999]:',
len(palinDates),
'\nFirst 15:',
'\n'.join(palinDates[0:15]),
'\nLast 15:',
'\n'.join(palinDates[-15:])
]:
print(x)
 
 
# MAIN ---
if __name__ == '__main__':
main()
Output:
Count of palindromic dates [2021..9999]:
284

First 15:
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12
2140-04-12

Last 15:
9170-07-19
9180-08-19
9190-09-19
9201-10-29
9210-01-29
9211-11-29
9220-02-29
9221-12-29
9230-03-29
9240-04-29
9250-05-29
9260-06-29
9270-07-29
9280-08-29
9290-09-29


Or, defined in terms of integer operations, rather than string reversals:

Works with: Python version 3.7
'''Palindrome dates'''
 
from functools import reduce
from itertools import chain
from datetime import date
 
 
# palinDay :: Integer -> [ISO Date]
def palinDay(y):
'''A possibly empty list containing the palindromic
date for the given year, if such a date exists.
'''

[m, d] = [undigits(pair) for pair in chunksOf(2)(
reversedDecimalDigits(y)
)]
return [] if (
1 > m or m > 12 or 31 < d
) else validISODate((y, m, d))
 
 
# --------------------------TEST---------------------------
# main :: IO ()
def main():
'''Count and samples of palindromic dates [2021..9999]
'''

palinDates = list(chain.from_iterable(
map(palinDay, range(2021, 10000))
))
for x in [
'Count of palindromic dates [2021..9999]:',
len(palinDates),
'\nFirst 15:',
'\n'.join(palinDates[0:15]),
'\nLast 15:',
'\n'.join(palinDates[-15:])
]:
print(x)
 
 
# -------------------------GENERIC-------------------------
 
# Just :: a -> Maybe a
def Just(x):
'''Constructor for an inhabited Maybe (option type) value.
Wrapper containing the result of a computation.
'''

return {'type': 'Maybe', 'Nothing': False, 'Just': x}
 
 
# Nothing :: Maybe a
def Nothing():
'''Constructor for an empty Maybe (option type) value.
Empty wrapper returned where a computation is not possible.
'''

return {'type': 'Maybe', 'Nothing': True}
 
 
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divible, the final list will be shorter than n.
'''

return lambda xs: reduce(
lambda a, i: a + [xs[i:n + i]],
range(0, len(xs), n), []
) if 0 < n else []
 
 
# reversedDecimalDigits :: Int -> [Int]
def reversedDecimalDigits(n):
'''A list of the decimal digits of n,
in reversed sequence.
'''

return unfoldr(
lambda x: Nothing() if (
0 == x
) else Just(divmod(x, 10))
)(n)
 
 
# unDigits :: [Int] -> Int
def undigits(xs):
'''An integer derived from a list of decimal digits
'''

return reduce(lambda a, x: a * 10 + x, xs, 0)
 
 
# unfoldr(lambda x: Just((x, x - 1)) if 0 != x else Nothing())(10)
# -> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
def unfoldr(f):
'''Dual to reduce or foldr.
Where catamorphism reduces a list to a summary value,
the anamorphic unfoldr builds a list from a seed value.
As long as f returns Just(a, b), a is prepended to the list,
and the residual b is used as the argument for the next
application of f.
When f returns Nothing, the completed list is returned.
'''

def go(v):
xr = v, v
xs = []
while True:
mb = f(xr[0])
if mb.get('Nothing'):
return xs
else:
xr = mb.get('Just')
xs.append(xr[1])
return xs
return lambda x: go(x)
 
 
# validISODate :: (Int, Int, Int) -> [Date]
def validISODate(ymd):
'''A possibly empty list containing the
ISO8601 string for a date, if that date exists.
'''

try:
return [date(*ymd).isoformat()]
except ValueError:
return []
 
 
# MAIN ---
if __name__ == '__main__':
main()
Output:
Count of palindromic dates [2021..9999]:
284

First 15:
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12
2140-04-12

Last 15:
9170-07-19
9180-08-19
9190-09-19
9201-10-29
9210-01-29
9211-11-29
9220-02-29
9221-12-29
9230-03-29
9240-04-29
9250-05-29
9260-06-29
9270-07-29
9280-08-29
9290-09-29

Raku[edit]

(formerly Perl 6)

Works with: Rakudo version 2020.01

Pretty basic, but good enough. Could start earlier but 3/2/1 digit years require different handling that isn't necessary for this task. (And would be pretty pointless anyway assuming we need 2 digits for the month and two digits for the day. ISO:8601 anybody?)

my $start = '1000-01-01';
 
my @palindate = {
state $year = $start.substr(0,4);
++$year;
my $m = $year.substr(2, 2).flip;
my $d = $year.substr(0, 2).flip;
next if not try Date.new("$year-$m-$d");
"$year-$m-$d"
}*;
 
my $date-today = Date.today; # 2020-02-02
 
my $k = @palindate.first: { Date.new($_) > $date-today }, :k;
 
say join "\n", @palindate[$k - 1 .. $k + 14];
 
say "\nTotal number of four digit year palindrome dates:\n" ~
my $four = @palindate.first( { .substr(5,1) eq '-' }, :k );
say "between {@palindate[0]} and {@palindate[$four - 1]}.";
 
my $five = @palindate.first: { .substr(6,1) eq '-' }, :k;
 
say "\nTotal number of five digit year palindrome dates:\n" ~
+@palindate[$four .. $five]
Output:
2020-02-02
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12
2140-04-12

Total number of four digit year palindrome dates:
331
between 1001-10-01 and 9290-09-29.

Total number of five digit year palindrome dates:
3303

REXX[edit]

This REXX version works with   Regina REXX.

The   date   BIF   (with the   base   argument)   converts a date to the number of years since the beginning of
the Gregorian calendar,   the date is in the   ISO   format   (International Standards Organization   8601:2004).

/*REXX program finds & displays the next  N  palindromic dates starting after 2020─02─02*/
/* ───── */
parse arg n from . /*obtain optional argumets from the CL*/
if n=='' | n=="," then n= 15 /*Not specified? Then use the default.*/
if from=='' | from=="," then from= '2020-02-02' /* " " " " " " */
#= 0 /*the count of palindromic dates so far*/
do j=date('Base', from, "ISO")+1 until #==n /*find palindromic dates 'til N found*/
aDate= date('ISO', j, "Base") /*convert a "base" date to ISO format. */
$= space( translate(aDate, , '-'), 0) /*elide the dashes (-) in this date. */
if $\==reverse($) then iterate /*Not palindromic? Then skip this date*/
say 'a palindromic date: ' aDate /*display a palindromic date ──► term. */
#= # + 1 /*bump the counter of palindromic dates*/
end /*j*/ /*stick a fork in it, we're all done. */
output   when using the default inputs:
a palindromic date:  2021-12-02
a palindromic date:  2030-03-02
a palindromic date:  2040-04-02
a palindromic date:  2050-05-02
a palindromic date:  2060-06-02
a palindromic date:  2070-07-02
a palindromic date:  2080-08-02
a palindromic date:  2090-09-02
a palindromic date:  2101-10-12
a palindromic date:  2110-01-12
a palindromic date:  2111-11-12
a palindromic date:  2120-02-12
a palindromic date:  2121-12-12
a palindromic date:  2130-03-12
a palindromic date:  2140-04-12

Ruby[edit]

require 'date'
 
palindate = Enumerator.new do |yielder|
("2020"..).each do |y|
m, d = y.reverse.scan(/../) # let the Y10K kids handle 5 digit years
strings = [y, m, d]
yielder << strings.join("-") if Date.valid_date?( *strings.map( &:to_i ) )
end
end
 
puts palindate.take(15)
Output:
2020-02-02
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12

Sidef[edit]

Translation of: Ruby
const Date = frequire('Date::Calc')
 
var palindates = gather {
for y in (2020 .. 9999) {
var (m, d) = Str(y).flip.last(4).split(2)...
take([y,m,d].join('-')) if Date.check_date(y, m, d)
}
}
 
say "Count of palindromic dates [2020..9999]: #{palindates.len}"
 
for a,b in ([
["First 15:", palindates.head(15)],
["Last 15:", palindates.tail(15)]
]) {
say ("\n#{a}\n", b.slices(5).map { .join(" ") }.join("\n"))
}
Output:
Count of palindromic dates [2020..9999]: 285

First 15:
2020-02-02   2021-12-02   2030-03-02   2040-04-02   2050-05-02
2060-06-02   2070-07-02   2080-08-02   2090-09-02   2101-10-12
2110-01-12   2111-11-12   2120-02-12   2121-12-12   2130-03-12

Last 15:
9170-07-19   9180-08-19   9190-09-19   9201-10-29   9210-01-29
9211-11-29   9220-02-29   9221-12-29   9230-03-29   9240-04-29
9250-05-29   9260-06-29   9270-07-29   9280-08-29   9290-09-29

zkl[edit]

TD,date,n := Time.Date, T(2020,02,02), 15;
while(n){
ds:=TD.toYMDString(date.xplode()) - "-";
if(ds==ds.reverse()){ n-=1; println(TD.toYMDString(date.xplode())); }
date=TD.addYMD(date,0,0,1);
}
Output:
2020-02-02
2021-12-02
2030-03-02
2040-04-02
2050-05-02
2060-06-02
2070-07-02
2080-08-02
2090-09-02
2101-10-12
2110-01-12
2111-11-12
2120-02-12
2121-12-12
2130-03-12