Palindrome dates: Difference between revisions

Added Swift solution
m (Replaced "UNIX shell" by "UNIX Shell" (to link the example to the existing category page))
(Added Swift solution)
Line 1,608:
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
</pre>
 
=={{header|Swift}}==
<lang swift>import Foundation
 
func isPalindrome(_ string: String) -> Bool {
let chars = string.lazy
return chars.elementsEqual(chars.reversed())
}
 
let format = DateFormatter()
format.dateFormat = "yyyyMMdd"
 
let outputFormat = DateFormatter()
outputFormat.dateFormat = "yyyy-MM-dd"
 
var count = 0
let limit = 15
let calendar = Calendar.current
var date = Date()
 
while count < limit {
if isPalindrome(format.string(from: date)) {
print(outputFormat.string(from: date))
count += 1
}
date = calendar.date(byAdding: .day, value: 1, to: date)!
}</lang>
 
{{out}}
Output when executed on 2020-07-26:
<pre>
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
</pre>
 
1,777

edits