![](/img/trans.png)
[英]Calculate in javascript time between 2 dates from input type date html
[英]Javascript: Calculate array of dates from date input
對於一個個人項目,我正在撓頭:
我想在未來輸入一個日期和一個數字,並返回一個在當前日期和未來日期之間均勻分割的日期數組(以任何格式)。
例如:今天是 11 月 6 日,我放了 1 Dec 和 n.5:函數(date, num) 以天為單位計算日期差異(我得到的這部分)並返回一個 nuum 日期數組(假設是天格式),每個在日期差異/數字時間。 (Vanilla/jquery/lodash/moment 等...都允許)
非常歡迎一些頭腦風暴或偽代碼!
編輯:我一開始不想分享,因為該應用程序目前已損壞並且需要一些時間才能運行,無論如何:
calculateReminders = (expirationDate, remindersNum) => {
let rightNow = moment();
let expirationDate = moment(expirationDate);
if (expirationDate !== null && remindersNum > 0) {
let differential = expirationDate.diff(rightNow, "days");
if (differential) {
let reminderFrequency = differential / remindersNum;
if (reminderFrequency < 1) {
Alert.alert(
"some alert"
);
return undefined;
}
}
}
}
}
我得到了日差,但我沒有創建我需要的后續日期數組。
這可能會給你你需要的東西。 希望能幫助到你。
const daysDifferential = 20; //This part you got const n = 5; // Number of dates to return const increaseOfDays = Math.floor(daysDifferential/n); const arr = []; for(let $i = 0; $i < n; $i++) { const d = new Date(); d.setDate(d.getDate() + $i * increaseOfDays) arr.push(d); } console.log(arr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.