score:1
maybe you can consider using of moment.js library https://github.com/moment/moment
notificationitems.filter(
(notification) =>
moment(notification.date).week() === moment().week() &&
moment(notification.date).year() === moment().year()
);
then by this code you will get only the notifications on this week
score:1
date.prototype.getweek = function() {
var date = new date(this.gettime());
date.sethours(0, 0, 0, 0);
// thursday in current week decides the year.
date.setdate(date.getdate() + 3 - (date.getday() + 6) % 7);
// january 4 is always in week 1.
var week1 = new date(date.getfullyear(), 0, 4);
// adjust to thursday in week 1 and count number of weeks from date to week1.
return 1 + math.round(((date.gettime() - week1.gettime()) / 86400000 - 3 + (week1.getday() + 6) % 7) / 7);
}
score:1
you could:
- first, get week of year by
.week()
- second, get year by
.year()
- finally, filter the date that has the same year and the same week in year with the current date
const notificationitems = [
{
id: 1,
date: "2021-02-10t05:04:59.525589z",
name: "glenn",
},
{
id: 2,
date: "2021-02-10t05:04:59.525589z",
name: "root",
},
{
id: 3,
date: "2021-01-08t05:04:59.525589z",
name: "smith",
},
{
id: 4,
date: "2019-02-03t05:04:59.525589z",
name: "ryan",
},
{
id: 5,
date: "2019-01-03t05:04:59.525589z",
name: "bob",
},
];
const sameyear = (date1, date2) =>
moment(date1).year() === moment(date2).year();
const sameweek = (date1, date2) =>
moment(date1).week() === moment(date2).week();
const sameweekinsameyear = (date1, date2) =>
sameyear(date1, date2) && sameweek(date1, date2);
const res = notificationitems.filter(({ date }) =>
sameweekinsameyear(date, new date())
);
console.log(res);
<script src="https://momentjs.com/downloads/moment.min.js"></script>
score:1
you have to create function with return number of week based on day number of first january and then use filter
const notificationitems = [
{
id: 1,
date: '2021-02-10t05:04:59.525589z',
name: 'glenn',
},
{
id: 2,
date: '2021-02-10t05:04:59.525589z',
name: 'root',
},
{
id: 3,
date: '2021-01-08t05:04:59.525589z',
name: 'smith',
},
{
id: 4,
date: '2019-02-03t05:04:59.525589z',
name: 'ryan',
},
{
id: 5,
date: '2019-01-03t05:04:59.525589z',
name: 'bob',
}
]
const getweek = date => {
let now = new date(date);
let firstjanuary = new date(now.getfullyear(), 0, 1);
return math.ceil( (((now.gettime() - firstjanuary.gettime()) / 86400000) + firstjanuary.getday() + 1) / 7 );
}
const currentweek = getweek(new date);
const currentweeknotificatoinitems = notificationitems.filter(item =>
getweek(new date(item.date)) === currentweek
)
console.log(currentweeknotificatoinitems)
score:1
try using
const notificationitems = [
{
id: 1,
date: '2021-02-10t05:04:59.525589z',
name: 'glenn'
},
{
id: 2,
date: '2021-02-10t05:04:59.525589z',
name: 'root'
},
{
id: 3,
date: '2021-01-08t05:04:59.525589z',
name: 'smith'
},
{
id: 4,
date: '2019-02-03t05:04:59.525589z',
name: 'ryan'
},
{
id: 5,
date: '2019-01-03t05:04:59.525589z',
name: 'bob'
}
];
let curr = new date; // get current date
let first = curr.getdate() - curr.getday();
let last = first + 6;
let firstday = new date(curr.setdate(first));
let lastday = new date(curr.setdate(last));
let a = notificationitems.filter((item) => {
let date = new date(item.date);
if(date.gettime() >= firstday.gettime() && date.gettime() <= lastday.gettime()) {
return item;
}
});
console.log(a);
hope this helps :)
Source: stackoverflow.com
Related Query
- How to get Current week from given array of objects date in javascript
- How to get array of objects in javascript from an array of dictionaries in python?
- How to get value based on key from an array of objects javascript
- How to get only latest value by date from array of objects in Js?
- How to reduce and get value from object array using javascript
- Get the max value for a date from an array of objects
- Javascript 2 arrays of objects , get a value from one array of objects and assign it to the other array of objects
- How to get the date ranges of a given week number in JS
- How to get the month and year from the past date untill now JavaScript
- How to Sort Dates, Numbers, Strings from the array of nested JSON objects in Javascript
- How to get values from one array of objects into another array of objects
- JavaScript how to get a length from a map array
- How to get an array of 10 objects from api for pagination in react
- How to get the id rather than labelKey from typeahead when an array of objects is passed through?
- React.js - How to get access to a specific value from an array of objects
- how to get javascript Current Date
- How to get 00:00; 10 September 2020 format from Date objects
- How to get current value onClick from an array of items in React js?
- How can I get the closest after date from the current date?
- How to get value based on key from an array of objects in React
- How get to All values matching the query from mongodoDb at Once from multiple documents having nested array of objects
- How to avoid empty objects in array from string separated by comma using JavaScript and NodeJS?
- Javascript how to get full two year from current month?
- How to pass an array of objects from an input box into a separate array in javascript
- How can I get a different set of objects from an array for each accordion item?
- How to find if one of the items have completed property with value false from an array of objects using javascript and react?
- How to find an object from recursive array of objects that is matching with the id using javascript and react?
- Get an array after filtering the data from the given values in javascript react
- How to get a count array of objects by month with javascript
- Javascript Redux - how to get an element from store by id
More Query from same tag
- updating a single object key value inside array of objects react redux state
- ReactJS : onClick in sub-component does not trigger
- Selecting an element in React in a stateless function component in React?
- Material-UI: Disable only the action icon for Chip
- How can I make my Bootstrap 4 columns fill the entire row width?
- chartjs doesn't render dates properly
- Show button when 2 inputs are full and valid in react native
- React/JavaScript Prevent onKeyDown typing (other langs)
- How to handle and redirect when blocked by CORS policy occurs in React application
- Navbar Elements
- Meteor react render subscribe
- Type '(props: Props) => Element[]' is not assignable to type 'FunctionComponent<Props>'
- Update specific object value contained within an array of objects in React
- Array and Objects of states in React
- Render item from an array after a fixed interval in React
- I want to change the size of the chakra ui drawer
- Cypress click on an nth image in a table with specifc src
- React component's defaultValue does not work
- Image require(url) from object doesn't display in React
- Warning: Can't perform a React state update on an unmounted component. useEffect cleanup function
- Bootstrap NavBar do not set up li class="active" with React-Router. ReactJS
- why are initial items not transitioned out using useTransition
- Change default Text Material-table
- Unable to pass prop back to parent component
- ReactJS onClick arguments not getting passed to parent component
- React Bootstrap Navbar hanging computer
- Error :: DELETE http://localhost:3000/users/todo/delete/undefined 404 (Not Found)
- ES6 ES2015 (Babel) Format / Beautfier for Sublime for .jsx .js
- I want to reset value to initial value formik
- How to pass multiple values of selected radio button in React JS