working with dates in react

How can i subtract 7 days from current date?
23 Replies
curator
curatorOPā€¢3y ago
i am currently doing this
const currentDate = new Date();
const lastWeekDate = new Date(currentDate - 7);
const currentDate = new Date();
const lastWeekDate = new Date(currentDate - 7);
Jochem
Jochemā€¢3y ago
alternatively you can do:
let date = new Date();
date.setDate(date.getDate() - 7);
let date = new Date();
date.setDate(date.getDate() - 7);
but yours seems to work as well
curator
curatorOPā€¢3y ago
react saying setDate is not a function
Jochem
Jochemā€¢3y ago
can you show the code you're using then? cause Date.setDate is part of the spec for the default Date object https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate
curator
curatorOPā€¢3y ago
okay got it working i did this
const currentDate = new Date();
let lastWeekDate = new Date();
lastWeekDate.setDate(lastWeekDate.getDate() - 7);
const currentDate = new Date();
let lastWeekDate = new Date();
lastWeekDate.setDate(lastWeekDate.getDate() - 7);
13eck
13eckā€¢3y ago
Date stamps are in milliseconds, so just:
const lastWeekDate = new Date(Date.now() - (1000 * 60 * 60 * 24 * 7));
const lastWeekDate = new Date(Date.now() - (1000 * 60 * 60 * 24 * 7));
1000ms per sec, 60s per min, 60m per hour, 24h per day, 7d per week Simple maths šŸ˜‰
Jochem
Jochemā€¢3y ago
that might break around DST there's about two hours a year where that could give you the wrong day
13eck
13eckā€¢3y ago
Well, fine, be that way lol
Jochem
Jochemā€¢3y ago
šŸ˜„ yours is fine for 99.99% of cases, but if setDate(lastWeekDate.getDate() - 7) doesn't work, then it's a bug in the engine, not your code šŸ˜‰
13eck
13eckā€¢3y ago
I like passing blame, yes Ship it!
curator
curatorOPā€¢3y ago
i want to achieve this: Week wise issue count (upto last 10 weeks) i have got the issues how can sort them on the ui up to last 10 weeks so basically i want to show how many issues are/were per week upto 10 weeks
Jochem
Jochemā€¢3y ago
how do you store your issues?
curator
curatorOPā€¢3y ago
i have fetched them, now there are like this: [100 issues] -> {issue.created_at} i hope u get this of hows the hierarchy
Jochem
Jochemā€¢3y ago
curator
curatorOPā€¢3y ago
wow, i thought u were writing something big šŸ˜…
Jochem
Jochemā€¢3y ago
got distracted šŸ™‚
curator
curatorOPā€¢3y ago
can u help me in this task?
Jochem
Jochemā€¢3y ago
I'll try to take a look tomorrow, it's very late here. Very globally, you'd want to use the method I linked on the array, to filter out any issues you don't want.
Unknown User
Unknown Userā€¢3y ago
Message Not Public
Sign In & Join Server To View
Jochem
Jochemā€¢3y ago
that looks like a nice library for filtering your issues, @curator, you could use isWithinInterval https://date-fns.org/v2.29.3/docs/isWithinInterval to determine if the issue's creation date is within the week you're looking at, and the subDays function Leon linked to create the two reference points. Something like:
const filtered = issues.filter(issue => isWithinInterval(new Date(issue.created_at), { start: subDays(new Date(), 7), end: new Date()}));
const filtered = issues.filter(issue => isWithinInterval(new Date(issue.created_at), { start: subDays(new Date(), 7), end: new Date()}));
curator
curatorOPā€¢3y ago
can i dm u?
Jochem
Jochemā€¢3y ago
no thanks if you have any questions you can ask them here, so that if anyone else ever needs this info, they can find it I'm glad to keep helping, but it's better for you too to keep it in the thread here so others can help too! @curator
curator
curatorOPā€¢3y ago
yes agreed
Want results from more Discord servers?
Add your server