I would like to get the occurences of a day in a specific week (the week that includes today). I have an array of visits with a specified location and date that I want to transform.
const today = new Date(Date.parse("2021-05-13")); // set today
const numDay = today.getDate();
const twoDaysAgo = new Date();
twoDaysAgo.setDate(numDay - 2); // set 2 days ago from today
const twelveDaysAgo = new Date();
twelveDaysAgo.setDate(numDay - 12); // set 12 days ago from today
const visits = [{
location: "Paris",
date: today
},
{
location: "Berlin",
date: twoDaysAgo
},
{
location: "Brussels",
date: twoDaysAgo
},
{
location: "Rome",
date: twelveDaysAgo
}
];
I would like to process the data so that the number of times a day occurs in the specific week can be tracked as a number and the locations are stored in an array. If there are no visits for a certain day, they do not need to be stored and it should just say numVisits: 0.
This is the outcome I would like to achieve.
const thisWeekResult = [{
monday: {
numVisits: 0
},
tuesday: {
numVisits: 2,
locations: ["Berlin", "Brussels"]
},
wednesday: {
numVisits: 0,
},
thursday: {
numVisits: 1,
locations: ["Paris"]
},
friday: {
numVisits: 0,
},
saturday: {
numVisits: 0,
},
sunday: {
numVisits: 0,
}
}];
You could start with the beginning of the week (start in the code below being sunday just gone).
Then you reduce the 7 days beyond that date, and look for records on that dat from the original set using filter. Finally you just build up the object required for each day
const today = new Date(Date.parse("2021-05-13")); // set today
const numDay = today.getDate();
const twoDaysAgo = new Date();
twoDaysAgo.setDate(numDay - 2); // set 2 days ago from today
const twelveDaysAgo = new Date();
twelveDaysAgo.setDate(numDay - 12); // set 12 days ago from today
const visits = [{
location: "Paris",
date: today
},
{
location: "Berlin",
date: twoDaysAgo
},
{
location: "Brussels",
date: twoDaysAgo
},
{
location: "Rome",
date: twelveDaysAgo
}
];
const start = new Date()
start.setDate(today.getDate() - today.getDay())
const result = [1,2,3,4,5,6,7].reduce( (acc,d) => {
const date = new Date()
date.setDate(start.getDate() + d);
const day = new Intl.DateTimeFormat('en', {weekday:'long'}).format(date).toLowerCase();
const records = visits.filter(v => v.date.getYear() == date.getYear() && v.date.getMonth() == date.getMonth() && v.date.getDate() == date.getDate());
acc[day] = records.length
? {
numVisits: records.length,
locations: records.map(r => r.location)
}
: {numVisits: 0 };
return acc;
},{});
console.log(result);
I'm building a function that creates a nested object with dynamic properties which has the year and month as keys.
const sixMonthSummary = {};
// This will get data for the latest 6 months
for (let i = 0; i <= 6; i++) {
const currentDate = new Date();
const [, month, year] = new Date(
currentDate.setMonth(currentDate.getMonth() - i)
)
.toLocaleDateString("en-SG")
.split("/");
sixMonthSummary[year] = {
[month]: {
rent: "",
income: "",
expenses: "",
},
};
}
console.log(sixMonthSummary)
The output only captures the last index and the first index instead
"2020": {
"07": {
"rent": "",
"income": "",
"expenses": ""
}
},
"2021": {
"01": {
"rent": "",
"income": "",
"expenses": ""
}
}
How do I make sure that the other months are not missed out?
You are overwriting the complete object-key at
sixMonthSummary[year] = {}
try to insert the existing object with a spread-operator to include all prev months.
const sixMonthSummary = {};
// This will get data for the latest 6 months
for (let i = 0; i <= 6; i++) {
const currentDate = new Date();
const [, month, year] = new Date(
currentDate.setMonth(currentDate.getMonth() - i)
)
.toLocaleDateString("en-SG")
.split("/");
sixMonthSummary[year] = {
...sixMonthSummary[year],
[month]: {
rent: "",
income: "",
expenses: "",
},
};
}
console.log(sixMonthSummary)
It's because you're resetting the year key each iteration of the loop. Try something like
if(!sixMonthSummary[year]) {
sixMonthSummary[year] = {};
}
sixMonthSummary[year][month] = {
rent: "",
income: "",
expenses: "",
};
I have last 30 days data getting from MS SQL server Database and i want to group JSON object based on week numbers using node JS . I have JSON object in below format
var data = [{name:"abc",status:"Pending",created_timestamp:"2020-01-12 12:00:12"},
{name:"abc1",status:"Pending",created_timestamp:"2020-12-12 12:00:12"},
{name:"abc2",status:"Pending",created_timestamp:"2020-17-12 12:00:12"},
{name:"abc3",status:"Rejected",created_timestamp:"2020-20-12 12:00:12"},
{name:"abc4",status:"Rejected",created_timestamp:"2020-22-12 12:00:12"}]
i want to get output in below format:
var graphDetails = [{Week:1,
Pending:1,
Rejected:0},
{Week:2,
Pending:2,
Rejected:0},
{Week:3,
Pending:1,
Rejected:1}...]
var data = [
{ name: "abc", status: "Pending", created_timestamp: "2020-01-12 12:00:12" },
{ name: "abc1", status: "Pending", created_timestamp: "2020-12-12 12:00:12" },
{ name: "abc2", status: "Pending", created_timestamp: "2020-17-12 12:00:12" },
{
name: "abc3",
status: "Rejected",
created_timestamp: "2020-20-12 12:00:12",
},
{
name: "abc4",
status: "Rejected",
created_timestamp: "2020-22-12 12:00:12",
},
];
function getWeekNumber(d) {
// Copy date so don't modify original
d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
// Set to nearest Thursday: current date + 4 - current day number
// Make Sunday's day number 7
d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay() || 7));
// Get first day of year
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
// Calculate full weeks to nearest Thursday
var weekNo = Math.ceil(((d - yearStart) / 86400000 + 1) / 7);
// Return array of year and week number
return [d.getUTCFullYear(), weekNo];
}
var updated = {};
data.forEach((element) => {
let week = getWeekNumber(new Date(element.created_timestamp));
let weekDetail = updated[week];
switch (element.status) {
case "pending":
weekDetail.pending = weekDetail.pending + 1;
break;
default:
weekDetail.rejected = weekDetail.rejected + 1;
break;
}
updated[week] = {
week: week,
pending: weekDetail.pending,
rejected: weekDetail.rejected,
};
});
Hope this will help you out from your problem
So I have the data in below format
const data = [
{ date: '01-07-2019' },
{ date: '02-07-2019' },
{ date: '03-07-2019' },
{ date: '04-07-2019' },
{ date: '05-07-2019' },
{ date: '06-07-2019' },
{ date: '07-07-2019' },
{ date: '08-07-2019' },
{ date: '09-07-2019' },
{ date: '10-07-2019' },
{ date: '15-07-2019' },
{ date: '16-07-2019' },
{ date: '20-07-2019' },
{ date: '21-07-2019' },
{ date: '22-07-2019' },
{ date: '23-07-2019' }
]
So I have to count the regular interval dates. For example on date { date: '10-07-2019' }, { date: '20-07-2019' } and on { date: '23-07-2019' } it breaks so count should be again started with 1.
const ouput = [{
startDate: '01-07-2019',
endDate: '10-07-2019',
count: 10
}, {
startDate: '15-07-2019',
endDate: '16-07-2019',
count: 2
}, {
startDate: '20-07-2019',
endDate: '23-07-2019',
count: 4
}]
I did that
const output = Object.values(data.reduce((a, { startDate, endDate }, i) => {
const startTime = moment(data[i].date)
const endTime = moment(data[i + 1] && data[i + 1].date)
if (moment.duration(endTime.diff(startTime)).asDays === 1) {
a.startDate = startDate
a.startDate = endDate
}
a.count++;
return a;
}, {}));
But it is not giving what I expect. Please help.
I would do that with a function generator to handle the desired aggregation.
The below code will loop the dates, take a pair, check whether the start date exists, update the end date and automatically yield the value if necessary.
Comments are directly in the code below, the code assumes the initial array is already sorted as the example you mentioned.
As a side note, you're actually including the last date in the count, while, effectively, it should be one day less than your count. Further comments about that are available below in the function generator code.
const data = [
{ date: '01-07-2019' },
{ date: '02-07-2019' },
{ date: '03-07-2019' },
{ date: '04-07-2019' },
{ date: '05-07-2019' },
{ date: '06-07-2019' },
{ date: '07-07-2019' },
{ date: '08-07-2019' },
{ date: '09-07-2019' },
{ date: '10-07-2019' },
{ date: '15-07-2019' },
{ date: '16-07-2019' },
{ date: '20-07-2019' },
{ date: '21-07-2019' },
{ date: '22-07-2019' },
{ date: '23-07-2019' }
];
// Counts intervals of consecutive dates.
function* countIntervals(dates) {
// declare an initial accumulator.
let acc = {
count: 0
};
for (let i = 0; i < dates.length; i++) {
// get the currently looped value and the next one.
const [curr, next] = [moment(dates[i].date, 'DD-MM-YYYY'), dates[i+1] ? moment(dates[i+1].date, 'DD-MM-YYYY') : null];
// if the current date and next days are valid and if the difference in days between them is 1..
if (curr && next && (next.diff(curr, "days") === 1)) {
// Then keep track of the start date if not set, update the end date and increase the count of days.
acc.startDate = acc.startDate || dates[i].date, acc.endDate = dates[i+1].date, acc.count++;
}
else {
// otherwise, if the accumulator has a start date, yield the value.
if (acc && acc.startDate) {
acc.count++; // <-- comment this if you don't want the last date to be included.
yield Object.assign({}, acc);
// and init again the accumulator.
acc = {
count: 0
};
}
}
}
// if the loop is finished and the progression continued, yield the current accumulator.
if (acc.startDate) yield acc;
}
// usage...
const intervals = [...countIntervals(data)];
console.log(intervals);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Here you go, Try this
const data = [
{ date: "01-07-2019" },
{ date: "02-07-2019" },
{ date: "03-07-2019" },
{ date: "04-07-2019" },
{ date: "05-07-2019" },
{ date: "06-07-2019" },
{ date: "07-07-2019" },
{ date: "08-07-2019" },
{ date: "09-07-2019" },
{ date: "10-07-2019" },
{ date: "15-07-2019" },
{ date: "16-07-2019" },
{ date: "20-07-2019" },
{ date: "21-07-2019" },
{ date: "22-07-2019" },
{ date: "23-07-2019" }
];
function to parse date
function parseDate(input) {
var parts = input.split("-");
// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
return new Date(parts[2], parts[1] - 1, parts[0]); // Note: months are 0-based
}
function to get date difference
function dateDiff(date1, date2) {
date1 = parseDate(date1);
date2 = parseDate(date2);
let diffTime = Math.abs(date2.getTime() - date1.getTime());
let diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
return diffDays;
}
Required output
const output = data.reduce(function(resultSet, currentValue, currentIndex, arr) {
if (resultSet.length == 0) {
resultSet.push({
startDate: currentValue.date,
endDate: currentValue.date,
count: 1
});
}
else{
let dateDiffrence = dateDiff(resultSet[resultSet.length-1].endDate, currentValue.date);
console.log(dateDiffrence);
if(dateDiffrence == 1){
resultSet[resultSet.length-1].endDate = currentValue.date;
resultSet[resultSet.length-1].count++;
}else{
resultSet.push({
startDate: currentValue.date,
endDate: currentValue.date,
count: 1
});
}
}
return resultSet;
}, []);
Yet another possible solution.
const parseDate = (str) => {
const [d, m, y] = str.split('-');
return +new Date(y, m - 1, d)
}
const output = data.reduce((a, {
date
}, i) => {
const cur = parseDate(date);
const lastDate = data[i - 1] && data[i - 1].date || date;
const last = parseDate(lastDate || date);
if (cur - last > 1000 * 60 * 60 * 24) a.push({count: 0});
const {
startDate = date,
count
} = a.pop();
a.push({
startDate,
endDate: date,
count: count + 1
})
return a;
}, [{
count: 0
}])
console.log (output)
<script>
const data = [
{ date: '01-07-2019' },
{ date: '02-07-2019' },
{ date: '03-07-2019' },
{ date: '04-07-2019' },
{ date: '05-07-2019' },
{ date: '06-07-2019' },
{ date: '07-07-2019' },
{ date: '08-07-2019' },
{ date: '09-07-2019' },
{ date: '10-07-2019' },
{ date: '15-07-2019' },
{ date: '16-07-2019' },
{ date: '20-07-2019' },
{ date: '21-07-2019' },
{ date: '22-07-2019' },
{ date: '23-07-2019' }
]</script>
If you construct UTC dates there will be no need to use moment.js. With UTC every day is 24 hours and DST does not apply. This solution features a boilerplate function to handle the creation of the UTC date from your date string format.
const data = [
{ date: '01-07-2019' },
{ date: '02-07-2019' },
{ date: '03-07-2019' },
{ date: '04-07-2019' },
{ date: '05-07-2019' },
{ date: '06-07-2019' },
{ date: '07-07-2019' },
{ date: '08-07-2019' },
{ date: '09-07-2019' },
{ date: '10-07-2019' },
{ date: '15-07-2019' },
{ date: '16-07-2019' },
{ date: '20-07-2019' },
{ date: '21-07-2019' },
{ date: '22-07-2019' },
{ date: '23-07-2019' }
];
const ONE_DAY = 1000 * 60 * 60 * 24;
function dateStrToUTC(dateStr) {
const dateParts = dateStr.split('-');
const utcDate = new Date();
utcDate.setUTCFullYear(dateParts[2]);
utcDate.setUTCMonth(dateParts[1] - 1);
utcDate.setUTCDate(dateParts[0]);
utcDate.setUTCHours(0);
utcDate.setUTCMinutes(0);
utcDate.setUTCSeconds(0);
utcDate.setUTCMilliseconds(0);
return utcDate;
}
function getRegularIntervals(accumulator, currentValue) {
const index = accumulator.length - 1;
let daysPassed = 0;
if (index > -1) {
daysPassed = (dateStrToUTC(currentValue.date) - dateStrToUTC(accumulator[index].endDate)) / ONE_DAY;
}
if (index > -1 && 1 == daysPassed) {
accumulator[index].endDate = currentValue.date;
accumulator[index].count++;
} else {
accumulator.push({
startDate: currentValue.date,
endDate: currentValue.date,
count: 1
});
}
return accumulator;
}
const output = data.reduce(getRegularIntervals, []);
console.log(output);
Output as expected:
[
{
"startDate": "01-07-2019",
"endDate": "10-07-2019",
"count": 10
},
{
"startDate": "15-07-2019",
"endDate": "16-07-2019",
"count": 2
},
{
"startDate": "20-07-2019",
"endDate": "23-07-2019",
"count": 4
}
]
I liked your approach of using reduce function.
Going with the same, I just added some more logic in there and here is the final code.
// initially lets assume first date is the start as well as end date
var dateIntervalObject = {
startDate: data[0].date,
endDate: data[0].date,
count: 1
};
var result = data.reduce((resultArray, obj, i) => {
if(i > 0) {
var startTime = moment(dateIntervalObject.endDate, "DD-MM-YYYY");
var endTime = moment(obj.date, "DD-MM-YYYY");
if (endTime.diff(startTime, 'days') === 1) {
dateIntervalObject.endDate = obj.date;
dateIntervalObject.count += 1;
// remove the latest object in array, to replace with new
resultArray.pop();
} else {
dateIntervalObject = {
startDate: obj.date,
endDate: obj.date,
count: 1
};
}
// push the date Interval object in the array
resultArray.push(dateIntervalObject);
}
return resultArray;
}, [dateIntervalObject]);
console.log('result: ',result);
Note:
When initialState of the accumulator is passed to reduce function it starts iterating from 0th index, which in our case have already been initialized in the dateIntervalObject and therefore the first iteration with index value 0 is skipped.
Also, if the interval is not changing, we don't need to add another object to our result array but instead update the end date of the last element of our result array. Therefore, first pop and then push to just update the end date and count value.
Hope this helps!
I currently have a form, how can i make it so the javascript won't execute if the form is not filled out? Like if these no title I would get an alert that says "Please fill out form". So I would alert the user to fill out the form before they can add a notification.
Heres my code so far.
let note = {
id: 0,
title: "",
text: "",
trigger: {
at: ""
},
badge: 0,
data: {
prop: "",
num: 0
}
}
let day = "";
let month = "";
let year = "";
let hour = "";
let minute = "";
let date = "";
document.getElementById("add-btn").onclick = () => {
let id = new Date().getTime();
let triggerDate = new Date(year, month, day, hour, minute);
let noteOptions = {
id: id,
title: note.title || "Default title",
text: note.text || "Default text",
at: triggerDate,
badge: 1,
data: {
prop: "prop value",
num: 42
}
};
cordova.plugins.notification.local.schedule(noteOptions);
localStorage.setItem(id, JSON.stringify(noteOptions));
navigator.notification.alert("Added notification id " + id + " will notify at: " + triggerDate.toString());
window.open("index.html");
}
document.getElementById("title").addEventListener("change", (event) => {
note.title = event.target.value;
});
document.getElementById("dateinput").addEventListener("blur", (event) => {
year = "", month = "", day = "";
date = new Date(event.target.value);
year = date.getFullYear();
month = date.getMonth();
day = date.getDate() + 1;
});
document.getElementById("timeinput").addEventListener("change", (event) => {
let time = event.target.value;
if(time) {
hour = (time.split(":")[0]);
minute = (time.split(":")[1]);
}
});
document.getElementById("text").addEventListener("change", (event) => {
note.text = event.target.value;
});
You can check if note.title and note.text have been set. If you need more alerts, you can keep putting more else if (condition).
document.getElementById("add-btn").onclick = () => {
if (!note.title) {
navigator.notification.alert('Missing title.');
}
else if (!note.text) {
navigator.notification.alert('Missing text.');
}
else {
let id = new Date().getTime();
let triggerDate = new Date(year, month, day, hour, minute);
let noteOptions = {
id: id,
title: note.title || "Default title",
text: note.text || "Default text",
at: triggerDate,
badge: 1,
data: {
prop: "prop value",
num: 42
}
};
cordova.plugins.notification.local.schedule(noteOptions);
localStorage.setItem(id, JSON.stringify(noteOptions));
navigator.notification.alert("Added notification id " + id + " will notify at: " + triggerDate.toString());
window.open("index.html");
}
}