Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ฟังก์ชัน Date.getFullYear() ใน JavaScript

Date.getFullYear() คืออะไร

เมธอด Date.prototype.getFullYear() คืนค่า ปี (year) แบบ 4 หลัก ของ Date object ตาม เวลาท้องถิ่น (local time) วิธีนี้แทนที่ getYear() ที่ deprecated และคืนค่าปี 2 หลัก (ยุค 1900-based)

ไวยากรณ์ (Syntax)

dateObj.getFullYear()

คืนค่า: จำนวนเต็มแทนปี (เช่น 2024, 1989, -500)

ตัวอย่างการใช้งาน

ตัวอย่างที่ 1: การใช้งานพื้นฐาน

const date = new Date('1989-09-26T00:04:00');
console.log(date.getFullYear()); // 1989

ตัวอย่างที่ 2: ปีปัจจุบัน

const now = new Date();
console.log(`ปีปัจจุบัน: ${now.getFullYear()}`); // 2024 (ขึ้นกับปีปัจจุบัน)

ตัวอย่างที่ 3: ปีก่อน ค.ศ. (BCE / Negative Years)

// ปี 500 ก่อน ค.ศ. = -500
const ancient = new Date(-500, 0, 1); // 1 มกราคม 500 BCE
console.log(ancient.getFullYear()); // -500

// ISO 8601 รองรับปีลบ
console.log(new Date('-0500-01-01').getFullYear()); // -500

UTC Version: getUTCFullYear()

const date = new Date('1989-09-26T12:04:25Z');

console.log('Local year:', date.getFullYear());     // ขึ้นกับ timezone
console.log('UTC year:', date.getUTCFullYear());    // 1989 เสมอ

getFullYear() vs getYear() (Deprecated)

เมธอดคืนค่าสถานะ
getFullYear()4 หลัก (2024, 1989, -500)✅ Standard
getYear()ปี - 1900 (124, 89, -2400)❌ Deprecated
const date = new Date('2024-01-15');
console.log(date.getFullYear()); // 2024
console.log(date.getYear());     // 124 (2024 - 1900) - อย่าใช้!

กรณีการใช้งานทั่วไป

  • Display year: แสดงปีใน UI, reports, documents
  • Age calculation: คำนวณอายุจากปีเกิด
  • Year-based logic: Fiscal year, academic year, leap year check
  • Copyright notices: อัปเดตปีอัตโนมัติ
  • Date validation: ตรวจสอบปีในช่วงที่ยอมรับได้

ตัวอย่าง: Age Calculation

function calculateAge(birthDate, referenceDate = new Date()) {
  const birth = new Date(birthDate);
  const ref = new Date(referenceDate);
  
  let age = ref.getFullYear() - birth.getFullYear();
  const monthDiff = ref.getMonth() - birth.getMonth();
  
  if (monthDiff < 0 || (monthDiff === 0 && ref.getDate() < birth.getDate())) {
    age--;
  }
  return age;
}

console.log(calculateAge('1990-05-15')); // อายุในปีปัจจุบัน
console.log(calculateAge('2000-02-29', '2024-02-28')); // 23 (ยังไม่ถึงวันเกิด)
console.log(calculateAge('2000-02-29', '2024-03-01')); // 24 (ผ่านวันเกิดแล้ว)

ตัวอย่าง: Leap Year Check

function isLeapYear(year) {
  return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
}

function getYearInfo(date = new Date()) {
  const year = date.getFullYear();
  return {
    year,
    isLeap: isLeapYear(year),
    daysInYear: isLeapYear(year) ? 366 : 365,
    nextLeap: isLeapYear(year) ? year : 
      Array.from({length: 10}, (_, i) => year + i + 1).find(y => isLeapYear(y))
  };
}

console.log(getYearInfo(new Date('2024-01-01'))); // { year: 2024, isLeap: true, ... }
console.log(getYearInfo(new Date('2023-01-01'))); // { year: 2023, isLeap: false, ... }

ข้อควรระวัง

  • คืนค่า local time - ใช้ getUTCFullYear() สำหรับ UTC
  • คืนค่า 4 หลักเสมอ (รองรับปีลบสำหรับ BCE)
  • Invalid Date -> คืนค่า NaN
  • อย่าใช้ getYear() - deprecated และสร้าง bug ง่าย (Y2K style)