ต้องการลบ 1 สัปดาห์ (7 วัน) จากวันที่ปัจจุบัน ใช้เมธอด setDate() และ getDate() ร่วมกัน
ไวยากรณ์
var anyVariableName = new Date(yourCurrentDate.setDate(yourCurrentDate.getDate() - 7))
ตัวอย่างโค้ด
var currentDate = new Date();
console.log("The current Date=" + currentDate);
var before7DaysDate = new Date(currentDate.setDate(currentDate.getDate() - 7));
console.log("The One week ago date=" + before7DaysDate);
วิธีรันโปรแกรม
ใช้คำสั่ง node fileName.js (เช่น node demo60.js)
ผลลัพธ์ตัวอย่าง
The current Date=Tue Jul 14 2020 19:12:43 GMT+0530 (India Standard Time)
The One week ago date=Tue Jul 07 2020 19:12:43 GMT+0530 (India Standard Time)