How to use the sub function from date-fns
Find comprehensive JavaScript date-fns.sub code examples handpicked from public code repositorys.
date-fns.sub subtracts a duration from a date, returning a new date.
142 143 144 145 146 147 148 149 150 151
exports.getProjectsCount = getProjectsCount; const reportDataCache = {}; async function getReportData(glam, forDate = new Date()) { if (dateFns.isToday(forDate)) { forDate = dateFns.sub(forDate, { days: 1 }); } const forDateString = dateFns.format(forDate, dbDateFormat); if (reportDataCache[glam.name]) { const report = reportDataCache[glam.name][forDateString];
+ 3 other calls in file
35 36 37 38 39 40 41 42 43 44 45
endOfWeek(start, {weekStartsOn: 1}) ]) } function getLastMonthRange(date) { const start = sub(startOfMonth(date), {months: 1}) return formatRange([ start, endOfMonth(start) ])
+ 15 other calls in file
How does date-fns.sub work?
date-fns.sub
is a function that subtracts a specified amount of time from a given date or time. The function takes two arguments: the date or time to be subtracted from and an object specifying the amount of time to subtract, which can include units such as years, months, days, hours, minutes, seconds, and milliseconds.
GitHub: vivekUnico/cms-vivek
49 50 51 52 53 54 55 56 57 58
temp.push({ isEnquiry: true }); } } let curr = new Date(); curr.setHours(0, 0, 0, 0); curr = sub(curr, { days: "30" }); if (Arr.length == 0) Arr.push({}); let data = await LeadAndEnquiry.aggregate([ { $lookup : { from: "staffs",
+ 73 other calls in file
GitHub: medberroug/proofBackend
462 463 464 465 466 467 468 469 470 471
}, async getKPIOrders() { const today = new Date(); const lastMonth = sub(today, { months: 1 }); const lastWeek = sub(today, { weeks: 1 }); const yesterday = sub(today, { days: 1 }); const thisMonthRange = { start: startOfMonth(today), end: endOfMonth(today) }; const lastMonthRange = { start: startOfMonth(lastMonth), end: endOfMonth(lastMonth) };
+ 5 other calls in file
Ai Example
1 2 3 4 5 6
const { sub } = require("date-fns"); const date = new Date(2022, 0, 1); // January 1, 2022 const newDate = sub(date, { days: 10 }); // December 22, 2021 console.log(newDate);
In the example above, we import the sub function from the date-fns library, create a new date object representing January 1st, 2022, and then subtract 10 days from it using the sub function. The resulting date is December 22nd, 2021.
date-fns.format is the most popular function in date-fns (3838 examples)