Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Phương thức Date.getUTCMinutes() trong JavaScript

Phương thức getUTCMinutes() trả về phút (0-59) của đối tượng Date theo thời gian UTC.

Cú pháp

dateObj.getUTCMinutes()
  • Trả về: Số nguyên 0-59
  • Không nhận tham số

Ví dụ cơ bản

const date = new Date('1989-09-26T12:04:25.096Z');
console.log(date.getUTCMinutes()); // 4

Ví dụ với múi giờ (GMT+11)

// 26/9/1989 12:04 GMT+11 = 26/9/1989 01:04 UTC
const date = new Date('1989-09-26T12:04:25+11:00');
console.log(date.getUTCMinutes()); // 4 (01:04 UTC -> 4 phút)

Ngày hiện tại UTC

const now = new Date();
console.log(now.getUTCMinutes()); // Phút UTC hiện tại

So sánh Local vs UTC Minutes

// UTC+7
const date = new Date('2024-01-01T06:30:00'); // 6:30 local = 23:30 UTC (ngày trước)

console.log("Local minutes:", date.getMinutes());     // 30
console.log("UTC minutes:",   date.getUTCMinutes());  // 30 (cùng phút, khác giờ/ngày)

const date2 = new Date('2024-01-01T06:30:45'); // Có giây
console.log("Local seconds:", date2.getSeconds());     // 45
console.log("UTC seconds:",   date2.getUTCSeconds());  // 45

Ứng dụng

  • Timestamp chuẩn UTC cho logging, database
  • So sánh thời gian cross-timezone
  • Lập lịch job chạy theo giờ UTC

Lưu ý

  • Phạm vi: 0-59
  • Khác với getMinutes() (local)
  • Trả về NaN nếu Date invalid
  • Cặp đôi với setUTCMinutes()