tengo un rango de fechas
Rango de fechas de ejemplo:
const startDate = "2022-06-02"; const endDate = "2022-06-20";
Quiero obtener las fechas que aparecen en la matriz de días proporcionada que se encuentra entre la fecha de inicio y la fecha de finalización
Ejemplo de matriz de días:
["tuesday", "friday", "saturday"]
El resultado esperado es:
2022-06-03 2022-06-04 2022-06-07 2022-06-10 2022-06-11 2022-06-14 2022-06-17 2022-06-18
¿Alguien puede ayudarme con esta lógica?
What I tried was so dirty, I put a loop on range of dates, and got the list of all dates, and then i put another loop to get name of day of each date, and then compared each day name in an array of days & pushed that date to new array
Aquí está el código (que funciona perfectamente bien) pero necesito una mejor solución :
function getDaysArray(start, end) { for(var arr=[],dt=new Date(start); dt<=new Date(end); dt.setDate(dt.getDate()+1)){ arr.push(helperClass.getDateTime(new Date(dt)).date); } return arr; } function getDayName (dateStr, locale){ var date = new Date(dateStr); return date.toLocaleDateString(locale, { weekday: 'long' }); } var days = ["tuesday", "friday", "saturday"]; var getAllDates = getDaysArray("2022-06-02", "2022-06-20"); var getDates = []; for(var i = 0; i < getAllDates.length; i++){ if(days.includes(getDayName(getAllDates[i]).toLowerCase())){ getDates.push(getAllDates[i]) } }
Si puede usar una biblioteca externa, la biblioteca date-fns proporciona algunos ayudantes para esto:
parse
: devuelve la fecha analizada de la cadena usando la cadena de formato dada.eachDayOfInterval
: devuelve la matriz de fechas dentro del intervalo de tiempo especificado.getDay
: obtiene el día de la semana de la fecha dada.is*
ayudantes, por ejemplo, isSunday
, isMonday
, etc.Entonces, si analiza sus fechas de inicio y finalización, obtiene un rango de intervalo completo, luego lo filtra contra los días, tendrá su lista.
import("https://unpkg.com/date-fns@2.28.0/esm/index.js").then(({ eachDayOfInterval, getDay, parse }) => { const startDate = parse("2022-06-02", "yyyy-MM-dd", new Date()); const endDate = parse("2022-06-20", "yyyy-MM-dd", new Date()); const daysOfWeek = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]; // Here is your list const daysToFilterFor = ["tuesday", "friday", "saturday"]; const daysToFilterForIndex = daysToFilterFor.map((day) => daysOfWeek.indexOf(day)); const range = eachDayOfInterval({ start: startDate, end: endDate, }).filter((day) => daysToFilterForIndex.includes(getDay(day))); console.log(range); });
Los detalles se comentan en el ejemplo.
// Array of days to filter const daysOff = ["Tuesday", "Friday", "Saturday"]; /** * @desc Determine the specific dates of days that match a given array * of weekdays within the range of two given dates. * @param {string<date>} start - The first date of the range * @param {string<date>} end - The last date of the range * @param {array<string>} weekdays - The list of days used to find the dates * @return {array<date>} An array of dates that correspond to weekdays */ function dateRanger(start, end, weekdays) { // Convert dates into timestamps {number} const A = Date.parse(start); const Z = Date.parse(end); // Seconds in a day const day = 86400000; // Determine number of days the range spans const total = Math.floor(((Z - A) / day) + 1); // Object that stores format settings for .toLocaleDateString() const options = { weekday: 'long', year: 'numeric', month: 'numeric', day: 'numeric' }; /* - Instantiate an Array with room to fit the range of dates - On each iteration instantiate a date based on the first date then format it and add it's weekday using .toLocaleDateString() - Return date string as an array of pairs using .split() */ const range = [...new Array(total)].map( (_, i) => new Date(A + (day * i)) .toLocaleDateString('en-US', options) .split(', ') ); /* - .flatMap() the first level of range to access the array of pairs - On each iteration compare the first value of each pair (sub[0]) with the given array (weekdays). - If there's a match, return the second value (sub[1]) of each pair - Otherwise return an empty array that gets flattened to zilch. */ return range.flatMap( sub => weekdays.flatMap( day => day === sub[0] ? sub[1] : [] ) ); } console.log(dateRanger('6-4-2022', '6-20-2022', daysOff));
Sé que esta puede no ser la mejor manera de hacerlo, pero así es como lo hice.
let days = { monday: 0, tuesday: 1, wednesday: 2, thursday: 3, friday: 4, saturday: 5, sunday: 6 }; const startDate = "2022-06-02"; const endDate = "2022-06-20"; function daysindates(arr) { let stdate = startDate.substr(-2); let endate = endDate.substr(-2); let myarr = [], result = [], finalresult = []; for (y of arr) { for (let i = parseInt(stdate) + days[y]; i <= parseInt(endate); i += 7) { result.push(i); } } finalresult = result.map((item) => { return "2022-06-" + item; }); console.log(finalresult); } daysindates(["monday", "tuesday"]);