JavaScript の一般的な日付計算関数
1. 2 つの期間の間の間隔を計算し、切り上げます。
computedDays(start, end) {
if (start && end) {
const day =
(new Date(end).getTime() - new Date(start).getTime()) /
3600 /
1000 /
24;
return parseInt(day);
}
},
2. 終了時間が開始時間より大きいかどうかを判断するために使用されます。
computedSrartIsBigEnd(start, end) {
if (start && end) {
if (new Date(end).getTime() - new Date(start).getTime() > 0) {
return true;
} else {
return false;
}
}
},
3. 現在の日付に基づいて n 日前を推定します。
nextDateByNum(date,n){
const nowTime = new Date(date);
nowTime.setTime(nowTime.getTime() + 3600 * 1000 * 24 * n);
return nowTime
}
4. 時刻と日付の書式設定に使用されます(2 桁未満の月と日は自動的に 0 で埋められます)。
handleData(date) {
const timeDate = new Date(date);
const year = timeDate.getFullYear();
const month = (timeDate.getMonth() + 1).toString().padStart(2, 0);
const day = timeDate.getDate().toString().padStart(2, 0);
const hour = timeDate.getHours().toString().padStart(2, 0);
const min = timeDate.getMinutes().toString().padStart(2, 0);
const s = timeDate.getSeconds().toString().padStart(2, 0);
return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + s;
},