時間を追加するには、現在の日付を取得し、ミリ秒単位で特定の時間を追加してから、次の値で新しい日付を作成します。
// get the current date & time (as milliseconds since Epoch)
const currentTimeAsMs = Date.now();
// Add 3 days to the current date & time
// I'd suggest using the calculated static value instead of doing inline math
// I did it this way to simply show where the number came from
const adjustedTimeAsMs = currentTimeAsMs + (1000 * 60 * 60 * 24 * 3);
// create a new Date object, using the adjusted time
const adjustedDateObj = new Date(adjustedTimeAsMs);
これをさらに説明するには; 動作しない理由dataObj.setMilliseconds()
は、dateobjのミリ秒PROPERTYを指定された値(0〜999の値)に設定するためです。オブジェクトの日付はミリ秒単位では設定されません。
// assume this returns a date where milliseconds is 0
dateObj = new Date();
dateObj.setMilliseconds(5);
console.log(dateObj.getMilliseconds()); // 5
// due to the set value being over 999, the engine assumes 0
dateObj.setMilliseconds(5000);
console.log(dateObj.getMilliseconds()); // 0
参照:
Date.now()
new Date()
Date.setMilliseconds()