Nodejs - Express - How to calculate the next closer time to today()? - javascript

I need to calculate which is the next closer hour to "time" taking into account the following array data:
var date = new Date();
var time = date.getHours(); // 17 -> it means 5:00 PM
var minute = date.getMinutes(); // 12
// This is how the data has been saved in the database.
{ id: ‘1’, time: '1:00 AM' }
{ id: ‘1’, time: '2:00 PM' }
{ id: ‘1’, time: '7:00 PM' }
{ id: ‘1’, time: '10:00 PM' }
{ id: ‘1’, time: '8:00 PM' }
{ id: ‘1’, time: '11:00 AM' }
{ id: ‘2’, time: '9:00 AM' }
{ id: ‘2’, time: '6:30 PM' }
{ id: ‘2’, time: '5:00 PM' }
{ id: ‘2’, time: '1:00 PM' }
The result need to be an array like this:
{id: ‘1’, time: '7:00 PM'}
{id: ‘2’, time: '6:30 PM'}
Basically I need to know which is the next closer time to 5:12 PM for each ID.
This is my code so far:
function calculateNextPill(items) {
let nextPillArr = [];
let itemData = null;
let item_id = null;
var currentTime = new Date();
var closerTime = new Date();
var newTimes = [];
for(i=0; i<items.length; i++) {
itemData = items[i].itemdata[0];
item_id = items[i]._id;
for (const prop in itemData.pills) {
const pill = itemData.pills[prop];
if (pill != undefined && pill.time != undefined) {
nextPillArr.push({id: item_id, time: pill.time});
}
}
}
nextPillArr.forEach(element => {
var time = element.time;
var scheduleTime = new Date();
var parts = time.match(/(\d+):(\d+) (AM|PM)/);
if (parts) {
var hours = parseInt(parts[1]),
minutes = parseInt(parts[2]),
tt = parts[3];
if (tt === 'PM' && hours < 12) hours += 12;
scheduleTime.setHours(hours, minutes, 0, 0);
var a = moment(currentTime);
var b = moment(scheduleTime);
b.diff(a);
newTimes.push({id: element._id, diff: b.diff(a)});
// here I need to calculate which time is closer for which pill. Not done yet. Need more coffe...
}
});
}

First you need a function that lets you get some kind of numeric value for each time that you can then use to compare the values. The following function will give us the amount of minutes in 24h format:
function time_to_numeric(time) {
const [_, h, m, meridian] = time.match(/(\d+):(\d+) (AM|PM)/);
let [hours, min] = [parseInt(h), parseInt(m)];
if (meridian === "PM" && hours !== 12) hours += 12;
if (meridian === "AM" && hours === 12) hours -= 12;
return hours * 60 + min;
}
Next, we also need the time in the same format for now:
const now = new Date();
const now_numeric = now.getHours() * 60 + now.getMinutes();
Using this, we can now start finding the closest times for each unique id assuming items is an array of all the objects in your example. This works by computing the difference in minutes to now and swapping the value if it lower. In the case of a time occuring earlier than now, we instead compute the difference to that time the next day. We save both the difference and the actual time for the current minimum for each id:
const closer_times_by_id = items.reduce((acc, {id, time}) => {
const time_numeric = time_to_numeric(time);
let diff = time_numeric - now_numeric;
if (diff < 0) diff = time_numeric + MINUTES_PER_DAY - now_numeric;
const prev_diff = acc[id] && acc[id].diff;
if (prev_diff === undefined || diff < prev_diff) {
acc[id] = { diff, time };
}
return acc;
}, {});
Now our closer_times_by_id will look something like {'1': {diff: 158, time: '7:00 PM'}, '2': {diff: 38, time: '5:00 PM'}}. We map this to an array in the following way:
times_arr = Object.entries(closer_times_by_id).map(item => {
const [id, { time }] = item;
return { id, time };
});
After this, we are done and times_arr contains your result.
Full code:
const MINUTES_PER_DAY = 24 * 60;
// Takes a string like '1:10 PM' and returns the amount of minutes in 24h format
function time_to_numeric(time) {
const [_, h, m, meridian] = time.match(/(\d+):(\d+) (AM|PM)/);
let [hours, min] = [parseInt(h), parseInt(m)];
if (meridian === "PM" && hours !== 12) hours += 12;
if (meridian === "AM" && hours === 12) hours -= 12;
return hours * 60 + min;
}
function closest_items_by_id(items) {
const now = new Date();
const now_numeric = now.getHours() * 60 + now.getMinutes();
// Find closest times for each id, giving preference to times in the
// future in case of ties
// After reducing has finished, closer_times_by_id will be an object like
// {'1': {diff: 158, time: '7:00 PM'}, '2': {diff: 38, time: '5:00 PM'}}
const closer_times_by_id = items.reduce((acc, {id, time}) => {
const time_numeric = time_to_numeric(time);
let diff = time_numeric - now_numeric;
// If time occured earlier than now, calculate diff to time next day
if (diff < 0) diff = time_numeric + MINUTES_PER_DAY - now_numeric;
const prev_diff = acc[id] && acc[id].diff;
if (prev_diff === undefined || diff < prev_diff) {
acc[id] = { diff, time };
}
return acc;
}, {});
// Map closer_times_by_id to desired format
return Object.entries(closer_times_by_id).map(item => {
const [id, { time }] = item;
return { id, time };
});
}
const raw_data = [
{ id: '1', time: '1:00 AM' },
{ id: '1', time: '11:00 AM' },
{ id: '1', time: '2:00 PM' },
{ id: '1', time: '7:00 PM' },
{ id: '1', time: '8:00 PM' },
{ id: '1', time: '10:00 PM' },
{ id: '2', time: '9:00 AM' },
{ id: '2', time: '1:00 PM' },
{ id: '2', time: '1:10 PM' },
{ id: '2', time: '5:00 PM' },
{ id: '2', time: '6:30 PM' },
]
const now = new Date();
console.log(`Time at SO-server: ${now.getHours()}:${now.getMinutes()}`);
console.log(closest_items_by_id(raw_data));

Related

Find missing months in js array

I have the following Array
[
{ Month: '2021-05', Count: 36 },
{ Month: '2021-06', Count: 1048 },
{ Month: '2021-07', Count: 572 },
{ Month: '2021-09', Count: 3 },
{ Month: '2021-12', Count: 52 },
{ Month: '2022-01', Count: 4 },
{ Month: '2022-02', Count: 273 },
{ Month: '2022-04', Count: 96 }
]
where I am missing a few months. I know how many months is needed (could be 12 or could be more or less) and I need the missing months (like 2021-08 in this case) to be added with a count of 0. How to go about it?
Here's a pure, functional approach which will create a new array with new items, inserting all of the missing months in order. The code includes some comments explaining the procedure:
const parseDate = str => str.split('-').map(Number);
const formatDate = (year, month) => `${year}-${String(month).padStart(2, '0')}`;
function createContinuousMonthCounts (array) {
const all = [];
// get initial year/month values from first item
let [year, month] = parseDate(array[0].Month);
const advanceDate = () => {
month += 1;
if (month > 12) {
year += 1;
month = 1;
}
};
for (const item of array) {
const [y, m] = parseDate(item.Month);
// while the current month is not equal to the current item's month,
// create an entry for the month, append it, and advance to the next month
while (year !== y || month !== m) {
all.push({Month: formatDate(year, month), Count: 0});
advanceDate();
}
// after we're up to date, add the current item and advance the date
all.push({...item});
advanceDate();
}
return all;
}
const array = [
{ Month: '2021-05', Count: 36 },
{ Month: '2021-06', Count: 1048 },
{ Month: '2021-07', Count: 572 },
{ Month: '2021-09', Count: 3 },
{ Month: '2021-12', Count: 52 },
{ Month: '2022-01', Count: 4 },
{ Month: '2022-02', Count: 273 },
{ Month: '2022-04', Count: 96 },
];
const all = createContinuousMonthCounts(array);
for (const {Month, Count} of all) console.log(Month, Count);
Just a shot into the dark (please consider adding some Code to your question):
const months = [
{ Month: '2021-05', Count: 36 },
{ Month: '2021-06', Count: 1048 },
{ Month: '2021-07', Count: 572 },
{ Month: '2021-09', Count: 3 },
{ Month: '2021-12', Count: 52 },
{ Month: '2022-01', Count: 4 },
{ Month: '2022-02', Count: 273 },
{ Month: '2022-04', Count: 96 }
];
const neededMonths = [
"2021-01","2021-02","2021-03","2021-04","2021-05","2021-06","2021-07","2021-08","2021-09","2021-10","2021-11","2021-12"
]
const missedMonths = [];
months.map( m => {
if(neededMonths.indexOf(m.Month) == -1 ){
missedMonths.push(m.Month);
}
});
console.log(missedMonths);
You first need a method to find all the months between a range, then iterate across all the months and add the missing ones with count: 0:
const months = [
{ Month: '2021-05', Count: 36 },
{ Month: '2021-06', Count: 1048 },
{ Month: '2021-07', Count: 572 },
{ Month: '2021-09', Count: 3 },
{ Month: '2021-12', Count: 52 },
{ Month: '2022-01', Count: 4 },
{ Month: '2022-02', Count: 273 },
{ Month: '2022-04', Count: 96 }
]
const firstMonth = months.at(0).Month;
const lastMonth = months.at(-1).Month;
const [initialYear, initialMonth] = firstMonth.split('-');
const [endingYear, endingMonth] = lastMonth.split('-');
const allMonths = [];
let currentMonth = initialMonth;
let currentYear = initialYear;
while (`${currentYear}-${(''+currentMonth).padStart(2, '0')}` !== lastMonth) {
allMonths.push(`${currentYear}-${(''+currentMonth).padStart(2, '0')}`);
currentMonth++;
if (currentMonth === 13) {
currentMonth = 1;
currentYear++;
}
}
allMonths.forEach(month => {
if (!months.find(m => m.Month === month)) {
months.push({Month: month, count: 0});
}
});
console.log(months);

Get only the last Date from an Array

I have an array of objects having a DateTime, like this:
[{Date1, Count1}, {Date2, Count2}, ...]
The Dates in the array are given by Hour (Date2 = Date1 + 1H), so I am interested in taking only the Date's last hour count.
{Date: 2020-03-21T20:00:00Z, Count: 3}
{Date: 2020-03-21T22:00:00Z, Count: 4}
{Date: 2020-03-21T23:00:00Z, Count: 15}
{Date: 2020-03-22T00:00:00Z, Count: 66}
{Date: 2020-03-22T01:00:00Z, Count: 70}
How can I reduce this Array to take in consideration only the last item of each day?
{Date: 2020-03-21T23:00:00Z, Count: 15}
{Date: 2020-03-22T01:00:00Z, Count: 70}
Something like myArray.groupBy(Date).TakeLast()...
Here's some code that only works if the dates are sorted (if they're not you can just sort via dates.sort((a, b) => a.Date.getTime() - b.Date.getTime()):
var dates = [
{ Date: new Date("2020-03-21T20:00:00Z"), Count: 3 },
{ Date: new Date("2020-03-21T22:00:00Z"), Count: 4 },
{ Date: new Date("2020-03-21T23:00:00Z"), Count: 15 },
{ Date: new Date("2020-03-22T00:00:00Z"), Count: 66 },
{ Date: new Date("2020-03-22T01:00:00Z"), Count: 70 }
];
var lastPerDay = [];
// just need to set to a value that's impossible to get normally
var prevDate = null;
// go backwards through the array to find the last instance
for (var i = dates.length - 1; i >= 0; i--) {
// need some way of combining year, month, and date into a value
var curDate = [dates[i].Date.getUTCFullYear(), dates[i].Date.getUTCMonth(), dates[i].Date.getUTCDate()].join(",");
// we haven't seen the date before
if (curDate !== prevDate) {
// add the day to the front
lastPerDay.unshift(dates[i]);
// update the previous date
prevDate = curDate;
}
}
console.log(lastPerDay);
With this, there is no need for the dates to be sorted.
let lastsHour = {}, array = [
{ date: new Date("2020-03-21T20:00:00Z"), count: 3 },
{ date: new Date("2020-03-21T22:00:00Z"), count: 4 },
{ date: new Date("2020-03-21T23:00:00Z"), count: 15 },
{ date: new Date("2020-03-22T00:00:00Z"), count: 66 },
{ date: new Date("2020-03-22T01:00:00Z"), count: 70 }
];
array.map(function (e) {
let currentDate = ""+e.date.getUTCDate()+e.date.getUTCMonth()+e.date.getUTCFullYear();
if (! lastsHour[currentDate]) {
lastsHour[currentDate] = e;
} else if (lastsHour[currentDate].date < e.date) {
lastsHour[currentDate] = e;
}
});
let result = [];
for (let key in lastsHour ) {
if (lastsHour.hasOwnProperty(key)) {
result.push(lastsHour[key]);
}
}
console.log(result);
We can use reduce method and decide on each iteration whether it is a next hour of current day. Then we can delete an array element which contains previous hour. We have O(N) by using reduce method:
const oneHourInMilliseconds = 3600000;
const result = arr.reduce((a, {Date: date, Count}) => {
let [y, m, d] = date.split(/\D+/);
let key = new Date(date).getTime();
a[key] = a[key] || { Date: date, Count };
if (a[key - oneHourInMilliseconds]) {
let [yPrev, mPrev, dPrev] = a[key - oneHourInMilliseconds].Date.split(/\D+/);
if (d == dPrev)
delete a[key-oneHourInMilliseconds];
}
return a;
},{})
console.log(Object.values(result));
An example:
let arr = [
{Date : '2020-03-21T22:00:00Z', Count: 4},
{Date : '2020-03-21T23:00:00Z', Count: 15},
{Date : '2020-03-22T00:00:00Z', Count: 66},
{Date : '2020-03-22T01:00:00Z', Count: 70},
];
const oneHourInMilliseconds = 3600000;
const result = arr.reduce((a, {Date: date, Count}) => {
let [y, m, d] = date.split(/\D+/);
let key = new Date(date).getTime();
a[key] = a[key] || { Date: date, Count };
if (a[key - oneHourInMilliseconds]) {
let [yPrev, mPrev, dPrev] = a[key - oneHourInMilliseconds].Date.split(/\D+/);
if (d == dPrev)
delete a[key-oneHourInMilliseconds];
}
return a;
},{})
console.log(Object.values(result));
var items = [
{ Date: new Date("2020-03-21T20:00:00Z"), Count: 3 },
{ Date: new Date("2020-03-21T22:00:00Z"), Count: 4 },
{ Date: new Date("2020-03-21T23:00:00Z"), Count: 15 },
{ Date: new Date("2020-03-22T00:00:00Z"), Count: 66 },
{ Date: new Date("2020-03-22T01:00:00Z"), Count: 70 },
{ Date: new Date("2020-03-22T20:00:00Z"), Count: 170 }
];
var filtered = items.filter((e, i, arr) => {
return (i == arr.length - 1 ||
arr[i].Date.toDateString() != arr[i + 1].Date.toDateString());
});
console.log(filtered);

convert array of objects to nested array of arrays for building chart

I have the following array
const data = [
{
date: "2018-01-01",
label: "MH",
qt: 10
},
{
date: "2018-04-01",
label: "MH",
qt: 30
},
{
date: "2018-02-01",
label: "GJ",
qt: 30
},
{
date: "2018-03-01",
label: "KL",
qt: 30
},
{
date: "2018-02-01",
label: "KL",
qt: 40
}
]
and i want my output to be
[
[date,MH,GJ,KL],
['Jan 2018',10,null,null],
['Feb 2018',null,30,40],
['Mar 2018',null,null,30],
['Apr 2018',30,null,null]
]
How can i achieve that in an optimize way?
And the date should be sorted in order to.
I tried doing
data.sort(function compare(a, b) {
var dateA = new Date(a.date);
var dateB = new Date(b.date);
return dateA - dateB;
});
let labelArr = data.map(l => l.label);
let dateArr = data.map(l => l.date);
labelArr = _.uniq(labelArr);
dateArr = _.uniq(dateArr);
console.log(labelArr, dateArr);
const outputArr = [];
dateArr.forEach(d => {
labelArr.forEach(l => {
const tempObj = data.filter(r => {
if (d == r.date && l == r.label) {
return r;
}
else {
return { date: d, label: l, qt: null }
}
})
outputArr.push(tempObj);
});
});
but i'm stuck here. What I was thinking is first i'll create the objects for date and label which are not present and add thier qt to null. After that i'll group by date and then insert only the qt to the result
You could take an object for keeping the array for each date and one for keeping track of the indices of the columns.
At the end set all elements to null for not set items.
var data = [{ date: "2018-01-01", label: "MH", qt: 10 }, { date: "2018-04-01", label: "MH", qt: 30 }, { date: "2018-02-01", label: "GJ", qt: 30 }, { date: "2018-03-01", label: "KL", qt: 30 }, { date: "2018-02-01", label: "KL", qt: 40 }],
cols = {},
rows = {},
result = data
.sort(({ date: a }, { date: b }) => a > b || -(a < b))
.reduce((r, { date, label, qt }) => {
date = date.slice(0, 7);
if (!rows[date]) r.push(rows[date] = [date]);
if (!cols[label]) cols[label] = r[0].push(label) - 1;
rows[date][cols[label]] = (rows[date][cols[label]] || 0) + qt;
return r;
}, [['date']])
.map((a, _, [{ length }]) => Array.from({ length }, (_, i) => a[i] || null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I don't know if it's the best solution, but it's a working solution:
const data = [{
date: "2018-01-01",
label: "MH",
qt: 10
},
{
date: "2018-04-01",
label: "MH",
qt: 30
},
{
date: "2018-02-01",
label: "GJ",
qt: 30
},
{
date: "2018-03-01",
label: "KL",
qt: 30
},
{
date: "2018-02-01",
label: "KL",
qt: 40
}
]
function formatData(data) {
const labels = {};
const map = {};
data
.sort((a, b) => a.date < b.date ? -1 : 1)
.forEach(item => {
const date = moment(item.date).format('MMM YYYY');
labels[item.label] = true;
if (map[date]) {
map[date][item.label] = item.qt;
} else {
map[date] = { [item.label]: item.qt };
}
});
const labelsArr = Object.keys(labels);
const formattedData = Object.keys(map).map(date => {
const values = labelsArr.map(label => map[date][label] || null);
return [date, ...values];
});
return [['date', ...labelsArr], ...formattedData];
}
const result = formatData(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://momentjs.com/downloads/moment.min.js"></script>
I tried doing this:
data.sort(function compare(a, b) {
var dateA = new Date(a.date);
var dateB = new Date(b.date);
return dateA - dateB;
});
let labelArr = data.map(l => l.label);
let dateArr = data.map(l => l.date);
labelArr = _.uniq(labelArr);
dateArr = _.uniq(dateArr);
const outputArr = [['Date', ...labelArr]];
dateArr.forEach(d => {
const o1 = data.filter(e => e.date == d)
const o2 = o1.reduce((s, a) => {
s[a.label] = a.qt;
return s;
}, {})
const b1 = []
labelArr.forEach(l => {
b1.push(o2[l])
});
outputArr.push([moment(d).format('MMM YYYY'), ...b1]);
});
Hope it helps someone.

Simple "if" statements

Recently I've been testing some things and I've stuck on little piece of code. Here's a sketch:
let data = [{
date: "2018-10-09 18:00",
temp: "+13"
},
{
date: "2018-10-09 21:00",
temp: "+12"
},
{
date: "2018-10-10 00:00",
temp: "+5"
},
{
date: "2018-10-10 15:00",
temp: "+18"
},
{
date: "2018-10-11 00:00",
temp: "+4"
},
{
date: "2018-10-11 00:00",
temp: "+4"
}
];
let res = [];
function setData(data, day, time, id) {
let a = {
day: day,
time: time,
temp: data.temp
}
res[id].push(a);
}
function parse(data) {
let day, time, id = -1;
for (let i = 0; i < data.length; i++) {
day = data[i].date.split(" ")[0];
time = data[i].date.split(" ")[1];
if (id !== 0 && id !== -1) {
setData(data[i], day, time, id);
} else {
id++;
res[id] = new Array();
setData(data[i], day, time, id);
}
}
}
parse(data);
console.log(res);
I need to parse data and sort different days in way like this:
res = [[{day: 2018-10-09}, {day: 2018-10-09}], [{day: 2018-10-10}, {day: 2018-10-10}], [{day: 2018-10-11}, {day: 2018-10-11}]]
But my code isn't right, and I've done so many attempts and I've failed. Please help me
You need to revise your approach, there's no logic that would group the dates in it. Try the following code instead:
let data = [
{
date: "2018-10-09 18:00",
temp: "+13"
},
{
date: "2018-10-10 00:00",
temp: "+5"
},
{
date: "2018-10-09 21:00",
temp: "+12"
},
{
date: "2018-10-10 15:00",
temp: "+18"
},
{
date: "2018-10-11 00:00",
temp: "+4"
},
{
date: "2018-10-11 00:00",
temp: "+4"
}
];
let res = {};
function setData(data, day, time){
let a = {
day: day,
time: time,
temp: data.temp
}
res[day].push(a);
}
function parse(data) {
let day, time, output = [];
for (let i = 0; i < data.length; i++) {
day = data[i].date.split(" ")[0];
time = data[i].date.split(" ")[1];
res[day] = res[day] || [];
setData(data[i], day, time);
}
let sortedDates = Object.keys(res).sort();
for (let date of sortedDates) {
output.push(res[date]);
}
return output;
}
res = parse(data);
console.log(res);
If you also want to sort the inner arrays then you can take a look at sort with compare function
Try this, hope it helps! Cheers
let data = [
{
date: "2018-10-09 18:00",
temp: "+13"
},
{
date: "2018-10-09 21:00",
temp: "+12"
},
{
date: "2018-10-10 00:00",
temp: "+5"
},
{
date: "2018-10-10 15:00",
temp: "+18"
},
{
date: "2018-10-11 00:00",
temp: "+4"
},
{
date: "2018-10-11 00:00",
temp: "+4"
}
];
let res = [];
function setData(data, day, time, id){
let a = {
day: day,
time: time,
temp: data.temp
}
res[id].push(a);
}
function parse(data) {
let day, time, lastDay, id = 0;
for (let i = 0; i < data.length; i++) {
day = data[i].date.split(" ")[0];
time = data[i].date.split(" ")[1];
if (i == 0) {
res.push([]);
setData(data[i], day, time, id);
} else {
lastDay = data[i-1].date.split(" ")[0];
if(day.localeCompare(lastDay) == 0) {
// Same day
setData(data[i], day, time, id);
} else {
// Another day
id++;
res.push([]);
setData(data[i], day, time, id);
}
}
}
}
parse(data);
console.log(res);
A slightly different approach by using a Map.
var data = [{ date: "2018-10-09 18:00", temp: "+13" }, { date: "2018-10-09 21:00", temp: "+12" }, { date: "2018-10-10 00:00", temp: "+5" }, { date: "2018-10-10 15:00", temp: "+18" }, { date: "2018-10-11 00:00", temp: "+4" }, { date: "2018-10-11 00:00", temp: "+4" }],
result = Array.from(
data
.reduce(
(m, { date, temp }) => (
([day, time]) => m.set(day, (m.get(day) || []).concat({ day, time, temp })))
(date.split(' ')),
new Map
)
.values(),
a => a.sort((a, b) => a.time.localeCompare(b.time))
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

filter array of objects based on month and year

I want to display the objects based on the current month and year
This filters the objects based on the month, I need to filter objects based on the year also.
var array = [{
title: "a",
date: "2018-03-29"
}, {
title: "b",
date: "2018-04-13"
}, {
title: "c",
date: "2018-04-12"
}, {
title: "leave",
date: "2018-04-11"
}, {
title: "d",
date: "2018-06-16"
}],
currentMonth = new Date().getMonth() + 1,
events = array.filter(e => {
var [_, month] = e.date.split('-'); // Or, var month = e.date.split('-')[1];
return currentMonth === +month;
});
console.log(events);
Well to filter by year and month, you just need to get the currentYear along with currentMonth, and then get the year and month of the iterated date.
This is how should be your code:
//Get the currentYear and the currentMonth
currentMonth = new Date().getMonth() + 1,
currentYear = new Date().getFullYear(),
//Get the year and month from the iterated date
var [year, month] = e.date.split('-');
//Then filter the dates
events = array.filter(e => {
var [year, month] = e.date.split('-'); // Or, var month = e.date.split('-')[1];
return (currentMonth === +month) && (currentYear == year);
});
Demo:
var array = [{
title: "a",
date: "2018-03-29"
}, {
title: "b",
date: "2018-04-13"
}, {
title: "c",
date: "2018-04-12"
}, {
title: "leave",
date: "2018-04-11"
}, {
title: "d",
date: "2018-06-16"
}],
currentMonth = new Date().getMonth() + 1,
currentYear = new Date().getFullYear(),
events = array.filter(e => {
var [year, month] = e.date.split('-'); // Or, var month = e.date.split('-')[1];
return (currentMonth === +month) && (currentYear == year);
});
console.log(events);
You can create a string representation of year and month like 2018-06 and check this value as a substring in date property to filter out the records of current year and current month.
var array = [{
title: "a",
date: "2018-03-29"
}, {
title: "b",
date: "2018-04-13"
}, {
title: "c",
date: "2018-04-12"
}, {
title: "leave",
date: "2018-06-11"
}, {
title: "d",
date: "2018-04-16"
},
{
title: "e",
date: "2018-06-18"
}],
currentMonth = '0'+(new Date().getMonth() + 1),
currentYear = new Date().getFullYear()
events = array.filter((e) => {
var dateStr = currentYear+'-'+currentMonth;
return (e.date.indexOf(dateStr) !== -1)
});
console.log(events);

Categories

Resources