Trước tiên, lấy ngày hiện tại:
var currentDateTime = new Date();
Để lấy ngày/giờ hiện tại tính bằng giây, sử dụng getTime()/1000.
Ví dụ
Mã minh họa:
var currentDateTime = new Date();
console.log("Ngày giờ hiện tại:");
console.log(currentDateTime);
var resultInSeconds = currentDateTime.getTime() / 1000;
console.log("Ngày giờ hiện tại tính bằng giây:");
console.log(resultInSeconds);Để chạy chương trình, sử dụng lệnh:
node tenFile.js
Tên file ở đây là demo285.js.
Kết quả
Đầu ra trên console (ví dụ):
Ngày giờ hiện tại: 2020-08-28T16:13:35.987Z Ngày giờ hiện tại tính bằng giây: 1598631215.987