原理很簡單了,直接上代碼了:
//測試
static void Main(string[] args)
{
List<DateTime> result = GetSymmetryDate(Convert.ToDateTime("2000-01-01"), Convert.ToDateTime("3000-01-01"));
foreach (DateTime datetime in result)
{
Console.WriteLine(datetime.ToString("yyyy-MM-dd"));
}
}
static List<DateTime> GetSymmetryDate(DateTime startDate, DateTime endDate)
{
List<DateTime> result = new List<DateTime>();
while (startDate <= endDate)
{
if (IsSymmetryDate(startDate))
{
result.Add(startDate);
}
startDate = startDate.AddDays(1);
}
return result;
}
//判斷是否為對稱日 原理:年反轉和月日匹配,匹配上了符合條件。
static bool IsSymmetryDate(DateTime dateTime)
{
string dateYY = dateTime.ToString("yyyyMMdd").Substring(0, 4);
string dateDDMM = dateTime.ToString("yyyyMMdd").Substring(4);
char[] arr = dateYY.ToCharArray();
Array.Reverse(arr);
string temp = new string(arr);
return dateDDMM == temp;
}
測試結果:
2001-10-02
2010-01-02
2011-11-02
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
2150-05-12
2160-06-12
2170-07-12
2180-08-12
2190-09-12
2201-10-22
2210-01-22
2211-11-22
2220-02-22
2221-12-22
2230-03-22
2240-04-22
2250-05-22
2260-06-22
2270-07-22
2280-08-22
2290-09-22