Phương thức getUTCSeconds() trả về giây (0-59) của đối tượng Date theo thời gian UTC (Coordinated Universal Time).
Cú pháp
dateObj.getUTCSeconds()
- 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'); // Z = UTC
console.log(date.getUTCSeconds()); // 25
Thiếu giây
const date = new Date('1989-09-26T12:04Z');
console.log(date.getUTCSeconds()); // 0 (mặc định)
Ngày hiện tại UTC
const now = new Date();
console.log(now.getUTCSeconds()); // Giây UTC hiện tại
Vượt phạm vi - Normalization
const date = new Date('1989-09-26T04:50:100Z'); // 100s = 1p40s
console.log(date.getUTCSeconds()); // 40
console.log(date.getUTCMinutes()); // 51
So sánh Local vs UTC
// Giả sử múi giờ UTC+7
const date = new Date('1989-09-26T12:04:25'); // Local time
console.log("Local seconds:", date.getSeconds()); // 25
console.log("UTC seconds:", date.getUTCSeconds()); // 25 (giống nhau nếu không đổi ngày)
const date2 = new Date('1989-09-26T23:30:00'); // Gần nửa đêm local
console.log("Local:", date2.getHours(), date2.getMinutes()); // 23, 30
console.log("UTC:", date2.getUTCHours(), date2.getUTCMinutes()); // 16, 30 (lùi 7h)
Ứng dụng
- Lưu trữ/compare timestamp chuẩn UTC
- Hệ thống đa múi giờ: server dùng UTC, client hiển thị local
- Log, audit trail với timestamp nhất quán
- Database: lưu UTC, hiển thị local
Lưu ý
- Phạm vi: 0-59
- Khác với
getSeconds()(local time) - Trả về
NaNnếu Date invalid - Cặp đôi với
setUTCSeconds()