I'm currently working in Node with mongoose as a database. I have two models product and customer .whenever a customer buy a product, the current Date gets stored in SellingDate attribute of the product. Now the thing I want to achieve is that when we give a random Date as input, it should compare it with all Selling dates of all products up to one year, and return us COUNT OF ALL DATES THAT LIE BETWEEN ONE YEAR. For example , if we have 5 records in database i.e
{
"Sun Feb 25 2015" , "Fri Mar 25 2015" , "Sat Dec 25 2015" , "Mon Feb 25 2016"
}
and I give " Sun Feb 25, 2015" as input it should return 2 as output because we have two dates between Feb 2015 - Feb 2016 .... and it should compare dates in String format because I store the date in "Day Month Date Year" format ...
You could do an aggregation where you first convert the date strings to a date using $toDate and also keep track of the year using $year. Then you can $match all documents within the given date range, and finally $group by the year and $count:
YourModel.aggregate([
{ "$addFields": {
"year":{ "$year": {
"$toDate": "$date"
}
} }},
{
$match: {
// define your range of years here, it would be 2015 - 2016 in your case
year: {$gte: 2016, $lte: 2020}
}
},
{
$group: {
_id: {year: "$year"}
}
},
{
$count: "totalCountPerYearRange"
}
]);
As long as the date strings are in an array - using [] NOT {} (which defines an object that requires property:value pairs), then you can convert the strings into dates and do simple comparisons. The following will return an array, where you can simply get the array's length for the count.
let myDates = [
"Thu Jan 07 2016",
"Tue Mar 01 2016",
"Wed May 25 2016",
"Sat Jun 25 2016",
"Sun Aug 14 2016",
"Fri Nov 18 2016",
"Fri Jan 06 2017",
"Sun Apr 16 2017",
"Sun May 21 2017",
"Sun Jul 30 2017",
"Fri Sep 22 2017",
"Sun Nov 12 2017",
"Sun Dec 24 2017"
]
let firstDate = new Date("Wed May 25 2016");
let lastDate = new Date("Thu May 25 2017");
let matches = myDates.filter(d => {if (new Date(d) >= firstDate && new Date(d) <= lastDate) return d});
console.log(matches);
console.log(matches.length);
I am currently using the date-fns library in my Angular application and I am using the helper called isWeekend to return some time slot items from an array that only fall on a weekend.
However, I can't find a way to return the Weekend items after a certain time such as 'after 1pm'.
Please see below:
There are two dates with a time after 13:00:00 (1 PM)
There are two dates with a time before 13:00:00 (1 PM)
I need to adjust my filter to accommodate for the times which are after 13:00:00 (1 PM) and on the weekend.
const days = [
{ date: "Sun Jul 25 2020 15:30:00 GMT+0100 (British Summer Time)" },
{ date: "Sun Jul 25 2020 19:00:00 GMT+0100 (British Summer Time)" },
{ date: "Sat Jul 25 2020 05:00:00 GMT+0100 (British Summer Time)" },
{ date: "Sat Jul 25 2020 02:00:00 GMT+0100 (British Summer Time)" }
];
const filteredDays = days.filter((day) => {
console.log(dateFns.isWeekend(day.date));
// Need a solution for dateFns.isWeekend and after 13:00:00 or 1 PM
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.min.js"></script>
You can extract the 'hours' component of the date with the same library's getHours function, and then see if the number it returns is 13 or higher. Combined with your existing usage of isWeekend, your filter is quite simple.
Note: This library is going to run both of those functions based on the user's current locale, while your dates are specifying a timezone. Just something to keep in mind.
const days = [
{ date: "Sun Jul 25 2020 15:30:00 GMT+0100 (British Summer Time)" },
{ date: "Sun Jul 25 2020 19:00:00 GMT+0100 (British Summer Time)" },
{ date: "Sat Jul 25 2020 05:00:00 GMT+0100 (British Summer Time)" },
{ date: "Sat Jul 25 2020 02:00:00 GMT+0100 (British Summer Time)" }
];
const filteredDays = days.filter((day) => {
return dateFns.isWeekend(day.date) && dateFns.getHours(day.date) >= 13;
});
console.log(filteredDays);
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.min.js"></script>
I have a multidimensional array whose first position is a Date() object. I have previously reduced the arrangement by grouping it per day. I try to reduce each daily sub-array with the results of the subtraction of the odd positions ("SALIDA") of the pairs ("ENTRADA").
These are the data:
const arrayHoras = [
[
"Thu Jan 02 2020 08:25:38 GMT+0100 (CET)",
"ENTRADA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Thu Jan 02 2020 13:30:43 GMT+0100 (CET)",
"SALIDA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Thu Jan 02 2020 15:18:06 GMT+0100 (CET)",
"ENTRADA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Thu Jan 02 2020 18:12:22 GMT+0100 (CET)",
"SALIDA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Fri Jan 03 2020 08:35:38 GMT+0100 (CET)",
"ENTRADA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Fri Jan 03 2020 10:15:23 GMT+0100 (CET)",
"SALIDA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Fri Jan 03 2020 10:59:16 GMT+0100 (CET)",
"ENTRADA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Fri Jan 03 2020 12:25:01 GMT+0100 (CET)",
"SALIDA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Fri Jan 03 2020 12:31:33 GMT+0100 (CET)",
"ENTRADA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Fri Jan 03 2020 13:20:43 GMT+0100 (CET)",
"SALIDA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Fri Jan 03 2020 15:28:06 GMT+0100 (CET)",
"ENTRADA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Fri Jan 03 2020 18:32:10 GMT+0100 (CET)",
"SALIDA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Tue Jan 07 2020 08:31:46 GMT+0100 (CET)",
"ENTRADA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Tue Jan 07 2020 13:22:51 GMT+0100 (CET)",
"SALIDA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Tue Jan 07 2020 15:30:00 GMT+0100 (CET)",
"ENTRADA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Tue Jan 07 2020 17:22:29 GMT+0100 (CET)",
"SALIDA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Tue Jan 07 2020 17:31:59 GMT+0100 (CET)",
"ENTRADA",
"NOMBRE APELLIDO APELLIDO"
],
[
"Tue Jan 07 2020 18:32:30 GMT+0100 (CET)",
"SALIDA",
"NOMBRE APELLIDO APELLIDO"
]
];
First step: we group the 2D arrangement by day the first position of each element
const gruposDias = arrayHoras.reduce((acumulador, valorActual) => {
const dia = new Date(valorActual[0]).getDate();
if (!acumulador[dia]) {
acumulador[dia] = [];
}
acumulador[dia].push(valorActual);
return acumulador;
}, []);
const agrupadosPorDia = Object.keys(gruposDias).map(porDia => {
return gruposDias[porDia];
});
Second step: I try to reduce the arrangements grouped by day with the result of subtracting the seconds elapsed between the dates of the odd positions ("SALIDA") of those of the even ("ENTRADA"):
const entradas = [];
const salidas = [];
for (let i = 0; i < agrupadosPorDia.length; i += 1) {
const agrupadosPorDiaI = agrupadosPorDia[i];
for (let j = 0; j < agrupadosPorDiaI.length; j += 1) {
if (j % 2) {
const salidaTime = new Date(agrupadosPorDia[i][j][0]).getTime();
salidas.push(salidaTime);
rowSalida.push((ultRowConDatoSimulado += 2));
} else {
const entradaTime = new Date(agrupadosPorDia[i][j][0]).getTime();
entradas.push(entradaTime);
}
}
}
console.log("entradas: ", entradas);
console.log("salidas: ", salidas);
My problem: with the second step I lose the return of the "entradas" and "salidas" arrays within each sub-array of the 2D array grouped by day.
Does anyone have the kindness to shed light on my decoder work?.
And abusing your knowledge, can you avoid the second step by expanding the array of the 2D array by expanding reduce () and map ()?. I've tried everything, I think.
Update
Expected result
agrupadosPorDia: [
[
[ "05:05:05" ],
[ "02:54:16" ]
],
[
[ "02:54:16" ],
[ "01:39:45" ],
[ "01:25:45" ],
[ "00:49:10" ],
[ "03:04:04" ]
],
[
[ "04:51:05" ],
[ "01:52:29" ],
[ "01:00:31" ],
]
];
As a single pass, you may assume that two consecutive horas make a call.
Then you map each call as a <diff, date> and group iteratively (like you did) the calls by date
Since you are not interested in the call but just the time it took (namely diff) you group the diff by date
const arrayHoras = [["Thu Jan 02 2020 08:25:38 GMT+0100 (CET)","ENTRADA","NOMBRE APELLIDO APELLIDO"],["Thu Jan 02 2020 13:30:43 GMT+0100 (CET)","SALIDA","NOMBRE APELLIDO APELLIDO"],["Thu Jan 02 2020 15:18:06 GMT+0100 (CET)","ENTRADA","NOMBRE APELLIDO APELLIDO"],["Thu Jan 02 2020 18:12:22 GMT+0100 (CET)","SALIDA","NOMBRE APELLIDO APELLIDO"],["Fri Jan 03 2020 08:35:38 GMT+0100 (CET)","ENTRADA","NOMBRE APELLIDO APELLIDO"],["Fri Jan 03 2020 10:15:23 GMT+0100 (CET)","SALIDA","NOMBRE APELLIDO APELLIDO"],["Fri Jan 03 2020 10:59:16 GMT+0100 (CET)","ENTRADA","NOMBRE APELLIDO APELLIDO"],["Fri Jan 03 2020 12:25:01 GMT+0100 (CET)","SALIDA","NOMBRE APELLIDO APELLIDO"],["Fri Jan 03 2020 12:31:33 GMT+0100 (CET)","ENTRADA","NOMBRE APELLIDO APELLIDO"],["Fri Jan 03 2020 13:20:43 GMT+0100 (CET)","SALIDA","NOMBRE APELLIDO APELLIDO"],["Fri Jan 03 2020 15:28:06 GMT+0100 (CET)","ENTRADA","NOMBRE APELLIDO APELLIDO"],["Fri Jan 03 2020 18:32:10 GMT+0100 (CET)","SALIDA","NOMBRE APELLIDO APELLIDO"],["Tue Jan 07 2020 08:31:46 GMT+0100 (CET)","ENTRADA","NOMBRE APELLIDO APELLIDO"],["Tue Jan 07 2020 13:22:51 GMT+0100 (CET)","SALIDA","NOMBRE APELLIDO APELLIDO"],["Tue Jan 07 2020 15:30:00 GMT+0100 (CET)","ENTRADA","NOMBRE APELLIDO APELLIDO"],["Tue Jan 07 2020 17:22:29 GMT+0100 (CET)","SALIDA","NOMBRE APELLIDO APELLIDO"],["Tue Jan 07 2020 17:31:59 GMT+0100 (CET)","ENTRADA","NOMBRE APELLIDO APELLIDO"],["Tue Jan 07 2020 18:32:30 GMT+0100 (CET)","SALIDA","NOMBRE APELLIDO APELLIDO"]]
// huge probability of being incorrect, use moment or something
const makeDelta = (a,b) => {
const d = new Date(b - a)
return [d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds()]
.map(x => (x+'').padStart(2, '0'))
.join(':')
}
const res = arrayHoras.reduce(({ dateToCalls, call }, h)=>{
call.push(h)
if (call.length == 2) {
const [a, b] = call.map(x => new Date(x[0]))
const date = a.getDate()
const v = dateToCalls.get(date) || []
v.push(makeDelta(a,b))
dateToCalls.set(date, v)
call = []
}
return { dateToCalls, call }
}, { dateToCalls: new Map(), call: []}).dateToCalls.values()
//res is an iterator, get an array
const out = [...res]
console.log(out)
We have an array of objects like this:
{key: "T62", currentWinnerName: "Test Register", dateCreated: Fri Jan 18 2019 18:17:50 GMT+0000 (Greenwich Mean Time)}
{key: "T68", currentWinnerName: "Test Register", dateCreated: Wed Jan 23 2019 14:57:40 GMT+0000 (Greenwich Mean Time)}
{key: "T58", currentWinnerName: "Test Register", dateCreated: Fri Jan 18 2019 15:57:45 GMT+0000 (Greenwich Mean Time)}
We need to sort the array based on the dateCreated value within the array.
I've tried various sort methods but unable to get the order to change.
if I understand correctly your object is array. then you can do like this way.
var obj = [
{
key: "T62",
currentWinnerName: "Test Register",
dateCreated: "Fri Jan 18 2019 18:17:50 GMT+0000 (Greenwich Mean Time)"
},
{
key: "T68",
currentWinnerName: "Test Register",
dateCreated: "Wed Jan 23 2019 14:57:40 GMT+0000 (Greenwich Mean Time)"
},
{
key: "T58",
currentWinnerName: "Test Register",
dateCreated: "Fri Jan 18 2019 15:57:45 GMT+0000 (Greenwich Mean Time)"}
];
let sortedObj = obj.sort(function(a,b){
return new Date(b.dateCreated) - new Date(a.dateCreated);
});
console.log(sortedObj);
Have you tried something like this :
const getTimestamp = (dateString)=> new Date(dateString).getTime();
const isOlder = (object1, object2)=> getTimestamp(object1.dateCreated) < getTimestamp(object2.dateCreated) ? -1 : 1;
const sortedArr = arr.sort(isOlder);
arr is your array. But you have to make dateCreated a string. This worked for me.
Hello folks I am trying to utilize the fullcalendar to display some holidays I have acquired from a json object.
JSON looks as follows.
var holidayObj = [
{
"holidayName" : "Boxing Day",
"holidayStart" : "May, 26 Oct 2014 13:00:00 EST",
"holidayEnd" : "May, 26 Oct 2014 13:00:00 EST"
}
{
"holidayName" : "Some other Day",
"holidayStart" : "May, 23 Oct 2014 13:00:00 EST",
"holidayEnd" : "May, 23 Oct 2014 13:00:00 EST"
}
];
My JS code to display the events on fullcalendar is as follows.
$.each(holidayObj, function(i, item) {
holidayNameText = item.holidayName;
console.log(holidayNameText); //"Boxing Day"
holidayStart = item.holidayStart; //May, 26 Oct 2014 13:00:00 EST
holidayEnd = item.holidayEnd; //May, 26 Oct 2014 13:00:00 EST
var eventObject = {
title: holidayNameText,
start: holidayStart,
end : holidayEnd,
allDay:true
};
$('#calendar').fullCalendar('renderEvent', eventObject, true);
console.log(eventObject.start);
});
For some reason the events don't seem to be populating on the calendar. Can anyone help me identify what the cause might may be?
Thank you.
You are missing a comma to separate the array elements:
var holidayObj = [{
"holidayName": "Boxing Day",
"holidayStart": "May, 26 Oct 2014 13:00:00 EST",
"holidayEnd": "May, 26 Oct 2014 13:00:00 EST"
}, {
"holidayName": "Some other Day",
"holidayStart": "May, 23 Oct 2014 13:00:00 EST",
"holidayEnd": "May, 23 Oct 2014 13:00:00 EST"
}];
By fixing it, it works fine (see the events in October).
Demo: http://jsfiddle.net/IrvinDominin/EGbHt/