Computer >> Máy Tính >  >> Lập trình >> Javascript

Nhận ngày / giờ hiện tại tính bằng giây - JavaScript?

Đầu tiên, hãy lấy ngày hiện tại -

var currentDateTime = new Date();

Để nhận ngày / giờ hiện tại tính bằng giây, hãy sử dụng getTime () / 1000.

Ví dụ

Sau đây là mã -

var currentDateTime = new Date();
console.log("The current date time is as follows:");
console.log(currentDateTime);
var resultInSeconds=currentDateTime.getTime() / 1000;
console.log("The current date time in seconds is as follows:")
console.log(resultInSeconds);

Để chạy chương trình trên, bạn cần sử dụng lệnh sau -

node fileName.js.

Đây, tên tệp của tôi là demo285.js.

Đầu ra

Điều này sẽ tạo ra kết quả sau trên bảng điều khiển -

The current date time is as follows:
2020-08-28T16:13:35.987Z
The current date time in seconds is as follows:
1598631215.987