Sử dụng Date.getTime() trả về milliseconds, chia 1000 và làm tròn để lấy giây.
Cú pháp
var currentDate = new Date();
var epochSeconds = Math.round(currentDate.getTime() / 1000);
Ví dụ
var currentDate = new Date();
var epochSeconds = Math.round(currentDate.getTime() / 1000);
console.log(epochSeconds);
Cách chạy
node demo.js
Kết quả mẫu
1594821507
Lưu ý: Math.floor() cũng được dùng thay Math.round() tùy yêu cầu làm tròn. Kết quả là số giây từ 00:00:00 UTC ngày 1/1/1970.