世界完全對稱日計算(C#)

來源:互聯網
上載者:User

原理很簡單了,直接上代碼了:

 

       //測試 

       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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.