เมธอดสแตติก DateTime.DaysInMonth(int year, int month) คืนจำนวนวันในเดือนที่ระบุของปีที่ระบุ คำนวณปีอธิกสุรทิน (Leap Year) อัตโนมัติ
รูปแบบการใช้งาน
int days = DateTime.DaysInMonth(2024, 2); // 29 (ปีอธิกสุรทิน)
int days = DateTime.DaysInMonth(2023, 2); // 28 (ปีปกติ)
โค้ดตัวอย่างสมบูรณ์
using System;
public class Demo
{
public static void Main()
{
Console.WriteLine($"วันนี้: {DateTime.Today:yyyy-MM-dd}");
// ตรวจสอบทุกเดือนในปี 2024 (ปีอธิกสุรทิน)
Console.WriteLine("\n=== จำนวนวันในปี 2024 (Leap Year) ===");
for (int month = 1; month <= 12; month++)
{
int days = DateTime.DaysInMonth(2024, month);
string monthName = new DateTime(2024, month, 1).ToString("MMMM", new System.Globalization.CultureInfo("th-TH"));
Console.WriteLine($" {month,2}: {monthName,-10} = {days} วัน");
}
// เปรียบเทียบปีอธิกสุรทินและปีปกติ
Console.WriteLine("\n=== กุมภาพันธ์: ปีอธิกสุรทิน vs ปีปกติ ===");
for (int year = 2020; year <= 2028; year++)
{
bool isLeap = DateTime.IsLeapYear(year);
int febDays = DateTime.DaysInMonth(year, 2);
Console.WriteLine($" {year}: {febDays} วัน {(isLeap ? "(อธิกสุรทิน)" : "")}");
}
// การใช้งานจริง: สร้างปฏิทิน
Console.WriteLine("\n=== สร้างปฏิทินเดือนนี้ ===");
int currentYear = DateTime.Today.Year;
int currentMonth = DateTime.Today.Month;
int daysInMonth = DateTime.DaysInMonth(currentYear, currentMonth);
DayOfWeek firstDayOfWeek = new DateTime(currentYear, currentMonth, 1).DayOfWeek;
Console.WriteLine($"{currentYear} 年 {currentMonth} 月 ({daysInMonth} วัน)");
Console.WriteLine($"เริ่มต้นวัน: {firstDayOfWeek}");
// ตรวจสอบวันที่เฉพาะ
Console.WriteLine($"\nวันที่ 31 มีในเดือนนี้หรือไม่: {daysInMonth >= 31}");
}
}
ผลลัพธ์
วันนี้: 2024-12-31
=== จำนวนวันในปี 2024 (Leap Year) ===
1: มกราคม = 31 วัน
2: กุมภาพันธ์ = 29 วัน
3: มีนาคม = 31 วัน
4: เมษายน = 30 วัน
5: พฤษภาคม = 31 วัน
6: มิถุนายน = 30 วัน
7: กรกฎาคม = 31 วัน
8: สิงหาคม = 31 วัน
9: กันยายน = 30 วัน
10: ตุลาคม = 31 วัน
11: พฤศจิกายน = 30 วัน
12: ธันวาคม = 31 วัน
=== กุมภาพันธ์: ปีอธิกสุรทิน vs ปีปกติ ===
2020: 29 วัน (อธิกสุรทิน)
2021: 28 วัน
2022: 28 วัน
2023: 28 วัน
2024: 29 วัน (อธิกสุรทิน)
2025: 28 วัน
2026: 28 วัน
2027: 28 วัน
2028: 29 วัน (อธิกสุรทิน)
=== สร้างปฏิทินเดือนนี้ ===
2024 年 12 月 (31 วัน)
เริ่มต้นวัน: Tuesday
วันที่ 31 มีในเดือนนี้หรือไม่: True
กฎปีอธิกสุรทิน (Leap Year Rules)
- ปีที่หารด้วย 4 ลงตัว → ปีอธิกสุรทิน
- ยกเว้นปีที่หารด้วย 100 ลงตัว → ไม่ใช่ปีอธิกสุรทิน
- ยกเว้นกฎข้างต้น: ปีที่หารด้วย 400 ลงตัว → ปีอธิกสุรทิน
// ตรวจสอบปีอธิกสุรทิน
bool isLeap = DateTime.IsLeapYear(2024); // true
bool isLeap2 = DateTime.IsLeapYear(1900); // false (หาร 100 ลงตัว แต่ไม่ใช่ 400)
bool isLeap3 = DateTime.IsLeapYear(2000); // true (หาร 400 ลงตัว)
การใช้งานจริง
// 1. Validation วันที่
public bool IsValidDate(int year, int month, int day)
{
if (month < 1 || month > 12) return false;
if (day < 1) return false;
return day <= DateTime.DaysInMonth(year, month);
}
// 2. วันที่สุดท้ายของเดือน
DateTime lastDayOfMonth = new DateTime(year, month, DateTime.DaysInMonth(year, month));
// 3. จำนวนวันทำงานในเดือน (ประมาณ)
int workingDays = DateTime.DaysInMonth(year, month) * 5 / 7; // Rough estimate
// 4. สร้างรายการวันที่ในเดือน
var datesInMonth = Enumerable.Range(1, DateTime.DaysInMonth(year, month))
.Select(d => new DateTime(year, month, d));
ข้อควรระวัง
- พารามิเตอร์
monthต้องเป็น 1-12 (โยนArgumentOutOfRangeExceptionหากนอกช่วง) - พารามิเตอร์
yearต้องเป็น 1-9999 (ช่วงของ DateTime) - คำนวณตามปฏิทิน Gregorian (ปฏิทินสากล) - ไทยใช้พุทธศักราชแต่ DateTime ใช้ ค.ศ.
- สำหรับปฏิทินอื่น (Hijri, Thai, ฯลฯ) ใช้
System.Globalization.Calendarแทน