うるう年を調べる方法・DaysInMonth †C#,VB(Visual Basic)で指定した年、月で閏年かどうかを調べる方法を紹介します。 参考サイト †関連記事 †DaysInMonthメソッドについて †DaysInMonthは引数に指定した年、月の日数を返却します。 サンプルコード †C#, Visual Basicで閏年を判定するサンプルコードを以下に記します。 C# サンプルコード †C#でDaysInMonthメソッドを利用したサンプルコードになります。 using System; namespace LeapCS { class Program { static void Main(string[] args) { Console.WriteLine("2019年2月の最終日 : " + DateTime.DaysInMonth(2019,2)); Console.WriteLine("2020年2月の最終日 : " + DateTime.DaysInMonth(2020, 2)); Console.WriteLine("2021年2月の最終日 : " + DateTime.DaysInMonth(2021, 2)); } } } Visual Basic サンプルコード †VBでDaysInMonthメソッドを利用したサンプルコードになります。 Module Module1 Sub Main() Console.WriteLine("2019年2月の最終日 : " & DateTime.DaysInMonth(2019, 2)) Console.WriteLine("2020年2月の最終日 : " & DateTime.DaysInMonth(2020, 2)) Console.WriteLine("2021年2月の最終日 : " & DateTime.DaysInMonth(2021, 2)) End Sub End Module サンプルコードの実行結果 †上記、C#,VBのサンプルコードを実行した時の出力になります。 以上、C#, Visual Basic(VB)で閏年かを調べるサンプルコードの紹介でした。 |