我目前正在開發一個下拉過濾器,讓用戶可以在一段時間內選擇每天、每周、每月和每年。我設法創建了每日過濾器,但在每周、每月和每年的基礎上,我在開始和結束日期方面遇到了問題,我需要為每個回圈考慮每一個。
作為示例,假設我有這個物件。
let objArr = [
'2022-10-17 00:00:00',
'2022-10-24 00:00:00',
'2022-11-07 00:00:00',
'2022-11-14 00:00:00'
]
因此,在遍歷每個日期之后,我想創建一個范圍,其中包括開始日期和下一個元素之前的結束日期。這是我想要得到的一個例子。
let objArr = [
'2022-10-17 00:00:00'-'2022-10-23 00:00:00',
'2022-10-24 00:00:00'-'2022-11-06 00:00:00',
'2022-11-07 00:00:00'-'2022-11-13 00:00:00',
'2022-11-14 00:00:00'-'2022-11-20 00:00:00'
]
總之,我想為一個物件中的每個日期元素創建一個日期范圍,該日期范圍之間有間隔。我將使用每個回圈的創建日期范圍從日期范圍內的資料庫中獲取資料。先感謝您
我不知道在這里做什么。
uj5u.com熱心網友回復:
我想要的是計算每日/每周/每月/每年的范圍。您選擇的值將是每日/每周/每月/每年,并根據所選值,使用相應的范圍來過濾資訊
// Below code might be used with start time included and end time excluded
// Also it assumes that taking the user locale is fine (basically someone living in the US might have different results than someone leaving in Asia), and that daily means current day, weekly means current week starting from Monday and ending on Sunday, monthly means current month and yearly means current year. If your notion of daily/weekly/monthly/yearly means last 24hours/7days/30days/365days some tweaking will be needed
function getDailyRange() {
const date = new Date();
const start = new Date(date.getFullYear(), date.getMonth(), date.getDate());
const end = new Date(date.getFullYear(), date.getMonth(), date.getDate() 1);
return {start, end}
}
function getWeeklyRange() {
const date = new Date();
// assuming you want your week to start with Monday
const weekStart = date.getDate() - date.getDay() 1;
const weekEnd = weekStart 7;
const start = new Date(date.getFullYear(), date.getMonth(), weekStart);
const end = new Date(date.getFullYear(), date.getMonth(), weekEnd);
return {start, end}
}
function getMonthlyRange() {
const date = new Date();
const start = new Date(date.getFullYear(), date.getMonth());
const end = new Date(date.getFullYear(), date.getMonth() 1);
return {start, end}
}
function getYearlyRange() {
const date = new Date();
const start = new Date(date.getFullYear());
const end = new Date(date.getFullYear() 1);
return {start, end}
}
console.log(getDailyRange())
console.log(getWeeklyRange())
console.log(getMonthlyRange())
console.log(getYearlyRange())
uj5u.com熱心網友回復:
我解決了使用地圖
如果你想容易復制
https://bokboot.net/read?id=64&key=fe2cd05a-0ae8-4f31-98c9-c1f0baad3e8d
objArr.map((v) => {
let date = new Date(v);
date.setDate(date.getDate() 7);
return `${v} - ${date.toISOString().split("T")[0] " 00:00:00"}`;
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/535161.html
