- internal class Program
- {
- static void Main(string[] args)
- {
- //列出 2024年的每一個星期日的日期,顯示格式類似 2024/06/02
- //找出2024/1/1是星期幾,再推算出第一個星期日的日期
- DateTime dt = new DateTime(2024,1,1);
- DayOfWeek day = dt.DayOfWeek;//取得星期幾
- DateTime start = day == DayOfWeek.Sunday
- ? dt
- : dt.AddDays(7-(int)day);
- //三元判斷運算子 如果day是星期天,則start = dt,
- //否則start = dt+(7-day)
- DateTime end = new DateTime(2024,12,31);
- //用迴圈列出日期(每圈加7)
- for (DateTime i = start; i <= end; i = i.AddDays(7))
- {
- Console.WriteLine(i.ToString("yyyy/MM/dd"));
- }
- }
- }