C# うるう年であるかを判定する

  • 作成日 2022.09.02
  • C#
C# うるう年であるかを判定する

C#で、うるう年であるかを判定するサンプルコードを記述してます。

環境

  • OS windows11 pro 64bit
  • Microsoft Visual Studio Community 2022 Version 17.2.6

うるう年であるかを判定

うるう年であるかを判定するには、「DateTime.IsLeapYear」を使用します。

using System;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {

            System.Console.WriteLine(DateTime.IsLeapYear(2020)); // true

            System.Console.WriteLine(DateTime.IsLeapYear(2021)); // false

            System.Console.WriteLine(DateTime.IsLeapYear(2022)); // false

        }

    }
}

実行結果