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

Làm cách nào để trừ ngày khỏi ngày hôm nay trong JavaScript?

Bạn cần sử dụng currentDate với sự trợ giúp của Date (). GetDate () mới. Cú pháp như sau -

 var anyVariableName =yourCurrentDate - yourSubstractDateOfCurrentMonth; 

Ví dụ

 var currentDate =new Date (). getDate (); var substractDate =new Date ("2020-07-01"). getDate (); const numberOfDaysInCurrentMonthOnly =currentDate-substractDate; console.log (numberOfDaysInCurrentMonthOnly);  

Lưu ý

 Ngày hôm nay là - 28-07-2020 

Để 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à demo129.js

 PS C:\ Users \ Amit \ JavaScript-code> bản demo nút129.js27