How to populate weeks number based on date occurance in javascript - javascript

I have a data like below:
************************************************************************************************
May - 2020
Date Week Week Count
Fri May 01 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 18 1
Sat May 02 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 18 1
Sun May 03 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 18 1+2 = 3
Mon May 04 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 19 1
Tue May 05 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 19 1
Wed May 06 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 19 1
Thu May 07 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 19 1
Fri May 08 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 19 1
Sat May 09 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 19 1
Sun May 10 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 19 1+6 = 7
Mon May 11 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 20 1
Tue May 12 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 20 1
Wed May 13 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 20 1
Thu May 14 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 20 1
Fri May 15 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 20 1
Sat May 16 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 20 1
Sun May 17 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 20 1+6 = 7
Mon May 18 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 21 1
Tue May 19 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 21 1
Wed May 20 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 21 1
Thu May 21 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 21 1
Fri May 22 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 21 1
Sat May 23 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 21 1
Sun May 24 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 21 1+6 = 7
Mon May 25 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 22 1
Tue May 26 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 22 1
Wed May 27 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 22 1
Thu May 28 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 22 1
Fri May 29 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 22 1
Sat May 30 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 22 1
Sun May 31 2020 00:00:00 GMT+0500 (Pakistan Standard Time) 22 1+6 = 7
***********************************************************************************************
And I like to arrange it like based on week occurances:
So final result will be like:
18=>3, 19=>7, 20=> 7, 21=>7, 22=>7
So in May, 18th week occured 1 time but 3 dates were occupied in `18th week of may 2020.
Similarly, same goes for other dates in stated above
The code I am trying is:
{
daysNumber.map((number, index) => {
let d = number.split('-');
NDate = new Date(d[0], d[1] - 1, d[2]);
weekNum = this.getWeekNumber(NDate);
})
}
getWeekNumber(now) {
let onejan = new Date(now.getFullYear(), 0, 1);
return Math.ceil((((now - onejan) / 86400000) + onejan.getDay()) / 7);
}
getDaysNumber(year, month) {
const dates = [];
const daysInMonth = new Date(year, month, 0).getDate();
for (let i = 1; i <= daysInMonth; i++) {
let parts = new Date(year + "-" + month + "-" + i);
dates.push(this.convertToDesiredDate(parts));
}
return dates;
}
convertToDesiredDate(str) {
let date = new Date(str),
month = ("0" + (date.getMonth() + 1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
return [date.getFullYear(), month, day].join("-");
}
Where daysNumber contains the month days starting from 01-31 ( for may ).
It will grow depends on the month.
I am stuck at populating them as an array to find how many times a week appeared in dates like my output.

Here is a version using luxon
const DateTime = luxon.DateTime;
const weekNums = days.reduce((obj, dateString) => {
let d = DateTime.fromJSDate(new Date(dateString))
const week = d.weekNumber;
console.log(d,d.weekdayLong,week)
obj[week] = (obj[week] + 1) || 1;
return obj;
}, {})
const days = `Fri May 01 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 02 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 03 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Mon May 04 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Tue May 05 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Wed May 06 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Thu May 07 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Fri May 08 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 09 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 10 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Mon May 11 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Tue May 12 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Wed May 13 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Thu May 14 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Fri May 15 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 16 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 17 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Mon May 18 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Tue May 19 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Wed May 20 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Thu May 21 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Fri May 22 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 23 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 24 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Mon May 25 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Tue May 26 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Wed May 27 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Thu May 28 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Fri May 29 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 30 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 31 2020 00:00:00 GMT+0500 (Pakistan Standard Time)`.split("\n")
// https://moment.github.io/luxon/docs/manual/tour.html#parse-from-iso-8601
// https://moment.github.io/luxon/docs/class/src/datetime.js~DateTime.html
const DateTime = luxon.DateTime;
const weekNums = days.reduce((obj, dateString) => {
let d = DateTime.fromJSDate(new Date(dateString))
const week = d.weekNumber;
console.log(d,d.weekdayLong,week)
obj[week] = (obj[week] + 1) || 1;
return obj;
}, {})
console.log(weekNums)
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.24.1/luxon.min.js"></script>
Using moment
const weekNums = days.reduce((obj, dateString) => {
let d = moment(dateString)
const week = d.isoWeek();
console.log(week,d.format('dddd'))
obj[week] = (obj[week] + 1) || 1;
return obj;
}, {})
const days = `Fri May 01 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 02 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 03 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Mon May 04 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Tue May 05 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Wed May 06 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Thu May 07 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Fri May 08 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 09 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 10 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Mon May 11 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Tue May 12 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Wed May 13 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Thu May 14 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Fri May 15 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 16 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 17 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Mon May 18 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Tue May 19 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Wed May 20 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Thu May 21 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Fri May 22 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 23 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 24 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Mon May 25 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Tue May 26 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Wed May 27 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Thu May 28 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Fri May 29 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 30 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 31 2020 00:00:00 GMT+0500 (Pakistan Standard Time)`.split("\n")
const weekNums = days.reduce((obj, dateString) => {
let d = moment(dateString)
const week = d.isoWeek();
console.log(week,d.format('dddd'))
obj[week] = (obj[week] + 1) || 1;
return obj;
}, {})
console.log(weekNums)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.25.3/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.25.3/locale/de.min.js"></script>
OLD ANSWER:
Using the elegant reduce
const weekNums = days.reduce((obj,dateString) => {
const week = getWeekNumber(new Date(dateString));
obj[week] = (obj[week]+1) || 1;
return obj;
},{})
PS: Week numbers start on the week that has the first thursday in the year
const days = `Fri May 01 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 02 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 03 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Mon May 04 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Tue May 05 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Wed May 06 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Thu May 07 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Fri May 08 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 09 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 10 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Mon May 11 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Tue May 12 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Wed May 13 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Thu May 14 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Fri May 15 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 16 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 17 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Mon May 18 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Tue May 19 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Wed May 20 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Thu May 21 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Fri May 22 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 23 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 24 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Mon May 25 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Tue May 26 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Wed May 27 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Thu May 28 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Fri May 29 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sat May 30 2020 00:00:00 GMT+0500 (Pakistan Standard Time)
Sun May 31 2020 00:00:00 GMT+0500 (Pakistan Standard Time)`.split("\n")
const getWeekNumber = now => {
let onejan = new Date(now.getFullYear(), 0, 1);
return Math.ceil((((now - onejan) / 86400000) + onejan.getDay()) / 7);
}
const getDaysNumber = (year, month) => {
const dates = [];
const daysInMonth = new Date(year, month, 0).getDate();
for (let i = 1; i <= daysInMonth; i++) {
let parts = new Date(year + "-" + month + "-" + i);
dates.push(this.convertToDesiredDate(parts));
}
return dates;
}
const weekNums = days.reduce((obj, dateString) => {
const week = getWeekNumber(new Date(dateString));
obj[week] = (obj[week] + 1) || 1;
return obj;
}, {})
console.log(weekNums)

Your question is a bit messy, but I'll do my best. I used your implementation of getWeekNumber, provided my own dates and wrote a simple reduce. If there's no entry for a particular week, we add it with a count of one, otherwise we increment and that's it. Learn more about Array.prototype.reduce here.
const dates = [
new Date('2000-01-01'),
new Date('2000-01-25'),
new Date('2000-02-01'),
new Date('2000-02-02'),
new Date('2000-02-03'),
];
const getWeekNumber = now => {
let onejan = new Date(now.getFullYear(), 0, 1);
return Math.ceil((((now - onejan) / 86400000) + onejan.getDay()) / 7);
}
const result = dates.reduce((countPerWeek, date) => {
const weekNumber = getWeekNumber(date);
if (!countPerWeek[weekNumber]) {
countPerWeek[weekNumber] = 1;
} else {
countPerWeek[weekNumber]++;
}
return countPerWeek;
}, {});
console.log(result);

Related

Google Apps Script return date between a range of dates in an array

I have an array of dates. I want to return the dates between January 01, 2022 and December 31, 2022.
When I run the cut below, it doesn't return anything. This is one of many different loop variations I've tried to no avail. Please help TIA
var start = new Date(2022, 0, 01); //returns Sat Jan 01 00:00:00 GMT-06:00 2022
var end = new Date(2022, 11, 01); //returns Sat Dec 31 00:00:00 GMT-06:00 2022
var arr = [Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 10 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 17 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 24 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 31 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 07 2022 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 14 2022 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 21 2022 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 28 2022 00:00:00 GMT-0600 (Central Standard Time),
...](**output** NOT the actual script)
for(var i=0; i<=arr.length; i++){
if(arr[i] >= start && arr[i] <= end){
Logger.log(arr[i]);
}
}
Modification points:
From your script, I couldn't understand the value of arr. Because in your script, when you save the script, an error occurs. So I'm worried that your actual script is different from your showing script. If each value of arr is the string type like Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time), please enclose it using the double or single quotes.
If my guess of your script is not correct, please provide your actual script.
In order to compare the date, I think that this thread will be useful. Ref
When these points are reflected in your script, it becomes as follows.
Modified script:
var start = new Date(2022, 0, 01); //returns Sat Jan 01 00:00:00 GMT-06:00 2022
var end = new Date(2022, 11, 01); //returns Sat Dec 31 00:00:00 GMT-06:00 2022
var arr = [
"Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Dec 10 2021 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Dec 17 2021 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Dec 24 2021 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Dec 31 2021 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Jan 07 2022 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Jan 14 2022 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Jan 21 2022 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Jan 28 2022 00:00:00 GMT-0600 (Central Standard Time)",
];
for (var i = 0; i <= arr.length; i++) {
if (new Date(arr[i]).getTime() >= start.getTime() && new Date(arr[i]).getTime() <= end.getTime()) {
console.log(arr[i]);
}
}
References:
Related thread
Compare two dates with JavaScript
What Tanaike mentioned is correct, it should be a string in your array and missing quotes then you can do
arr.filter(dates => new Date(dates) >= start && new Date(dates) <= end );
I was able to figure it out thanks for your input. The problem was I needed to set arr[i] to new Date(arr[i]). Below is the updated code.
var start = new Date(2022, 0, 01); //returns Sat Jan 01 00:00:00 GMT-06:00 2022
var end = new Date(2022, 11, 01); //returns Sat Dec 31 00:00:00 GMT-06:00 2022
var arr = [Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 10 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 17 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 24 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 31 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 07 2022 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 14 2022 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 21 2022 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 28 2022 00:00:00 GMT-0600 (Central Standard Time),
...](**output of the array** NOT the actual script)
for(var i=0; i<=arr.length; i++){
var arrI = new Date(arr[i]);
if(arrI >= start && arrI <= end){
Logger.log(arrI);
}
}

moment js bug for certain dates. moment.year(x).week(y).endOf("week")

I am getting some weird output for certain dates for moment js.
It is only for the dates 1st, 2nd and 3rd of Jan 2022 otherwise it works as expected.
I would expect the same output regardless of the current date.
Any idea why this is occurring?
console.log('Test 2019', moment().year(2019).week(48).endOf("week"));
console.log('Test 2020', moment().year(2020).week(48).endOf("week"));
console.log('Test 2021', moment().year(2021).week(48).endOf("week"));
console.log('Test 2022', moment().year(2022).week(48).endOf("week"));
console.log('Test 2023', moment().year(2023).week(48).endOf("week"));
// With date of device set to the future 3/Jan/2022
Test 2019 Sun Dec 09 2018 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2020 Sun Dec 08 2019 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2021 Sun Dec 06 2020 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2022 Sun Dec 04 2022 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2023 Sun Dec 03 2023 23:59:59 GMT+1300 (New Zealand Daylight Time)
// With date of device set to the today 15/dec/2021
Test 2019 Sun Dec 08 2019 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2020 Sun Dec 06 2020 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2021 Sun Dec 05 2021 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2022 Sun Dec 04 2022 23:59:59 GMT+1300 (New Zealand Daylight Time)
Test 2023 Sun Dec 03 2023 23:59:59 GMT+1300 (New Zealand Daylight Time)
Updated test:
console.log('Test 2019 B', moment().year(2019));
console.log('Test 2020 B', moment().year(2020));
console.log('Test 2021 B', moment().year(2021));
console.log('Test 2022 B', moment().year(2022));
console.log('Test 2023 B', moment().year(2023));
// With date of device set to auto / today 15/dec/2021
Test 2019 B Sun Dec 15 2019 15:14:54 GMT+1300 (New Zealand Daylight Time)
Test 2020 B Tue Dec 15 2020 15:14:54 GMT+1300 (New Zealand Daylight Time)
Test 2021 B Wed Dec 15 2021 15:14:54 GMT+1300 (New Zealand Daylight Time)
Test 2022 B Thu Dec 15 2022 15:14:54 GMT+1300 (New Zealand Daylight Time)
Test 2023 B Fri Dec 15 2023 15:14:54 GMT+1300 (New Zealand Daylight Time)
// With date of device set to the future 3/Jan/2022
Test 2019 B Thu Jan 03 2019 15:21:48 GMT+1300 (New Zealand Daylight Time)
Test 2020 B Fri Jan 03 2020 15:21:48 GMT+1300 (New Zealand Daylight Time)
Test 2021 B Sun Jan 03 2021 15:21:48 GMT+1300 (New Zealand Daylight Time)
Test 2022 B Mon Jan 03 2022 15:21:48 GMT+1300 (New Zealand Daylight Time)
Test 2023 B Tue Jan 03 2023 15:21:48 GMT+1300 (New Zealand Daylight Time)
console.log('Test 2019 C', moment().year(2019).week(48));
console.log('Test 2020 C', moment().year(2020).week(48));
console.log('Test 2021 C', moment().year(2021).week(48));
console.log('Test 2022 C', moment().year(2022).week(48));
console.log('Test 2023 C', moment().year(2023).week(48));
// With date of device set to the future 3/Jan/2022
Test 2019 C Thu Dec 06 2018 15:26:35 GMT+1300 (New Zealand Daylight Time)
Test 2020 C Fri Dec 06 2019 15:26:35 GMT+1300 (New Zealand Daylight Time)
Test 2021 C Sun Dec 06 2020 15:26:35 GMT+1300 (New Zealand Daylight Time)
Test 2022 C Mon Nov 28 2022 15:26:35 GMT+1300 (New Zealand Daylight Time)
Test 2023 C Tue Nov 28 2023 15:26:35 GMT+1300 (New Zealand Daylight Time)

Grouping dates by months in Javascript

How can I group a array of dates to get a new array of objects with grouped dates by months in Javascript
I have:
const arr = [
Date Sat Dec 31 2016 01:00:00 GMT+0100 (Central European Standard Time),
​Date Sun Jan 01 2017 01:00:00 GMT+0100 (Central European Standard Time),
​Date Mon Jan 02 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Tue Jan 31 2017 01:00:00 GMT+0100 (Central European Standard Time),
​​Date Wed Feb 01 2017 01:00:00 GMT+0100 (Central European Standard Time),
​​Date Thu Feb 02 2017 01:00:00 GMT+0100 (Central European Standard Time),
​​Date Fri Feb 03 2017 01:00:00 GMT+0100 (Central European Standard Time),
​​Date Sat Feb 04 2017 01:00:00 GMT+0100 (Central European Standard Time),
​​Date Sun Feb 05 2017 01:00:00 GMT+0100 (Central European Standard Time),
​​Date Mon Feb 06 2017 01:00:00 GMT+0100 (Central European Standard Time),
​​Date Tue Feb 07 2017 01:00:00 GMT+0100 (Central European Standard Time),
​​Date Wed Feb 08 2017 01:00:00 GMT+0100 (Central European Standard Time),
​​Date Thu Feb 09 2017 01:00:00 GMT+0100 (Central European Standard Time),
​​Date Fri Feb 10 2017 01:00:00 GMT+0100 (Central European Standard Time),
]
And I want to get:
const nArr = [
{
name: December,
dates:[
Date Sat Dec 31 2016 01:00:00 GMT+0100 (Central European Standard Time)
]
},
{
name: January,
dates:[
​ Date Sun Jan 01 2017 01:00:00 GMT+0100 (Central European Standard Time),
​ Date Mon Jan 02 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Tue Jan 31 2017 01:00:00 GMT+0100 (Central European Standard Time),
]
}
....
]
I try to use array reducer, but I have problem with it
arr.reduce((acc, val) => {
const d = new Date(val)
let m = months[d.getMonth()]
acc[m] = acc[m]
acc[m].push(val)
return acc
}, [])
Thank you for help, I'm new in JS
const dates = [new Date(), new Date()];
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
const sortedDates: Array<{name: string, dates: Array<Date>}> = [];
dates.map(date => {
const month = months[date.getMonth()];
const monthObj = sortedDates.find(datesByMonth => datesByMonth.name === month)
if (monthObj === undefined) {
sortedDates.push({
name: month,
dates: [
date
]
})
return;
}
monthObj.dates.push(date);
});
I'm not an expert but this is how I would have solved it
const arr = [
new Date('Date Sat Dec 31 2016 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Sun Jan 01 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Mon Jan 02 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Tue Jan 31 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Wed Feb 01 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Thu Feb 02 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Fri Feb 03 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Sat Feb 04 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Sun Feb 05 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Mon Feb 06 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Tue Feb 07 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Wed Feb 08 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Thu Feb 09 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Fri Feb 10 2017 01:00:00 GMT+0100 (Central European Standard Time'),
];
let months = [];
for (var i = 0; i < arr.length; i++) {
var month = arr[i].getMonth();
if (!months[month]) {
months[month] = [];
}
months[month].push(arr[i]);
}
console.log(months);
You can get the full month "name" by formatting the locale string with the month:long option.
const toLongMonth = (date) =>
date.toLocaleString('en-US', { month: 'long' });
const groupBy = (arr, fn) => arr.reduce((acc, item) =>
(month => ({
...acc,
[month]: [ ...(acc[month] || []), item ]
}))
(fn(item)), {});
const groupByMonth = (dates) =>
Object.entries(groupBy(dates, toLongMonth))
.map(([name, dates]) => ({ name, dates }))
const dates = [
new Date('Sat Dec 31 2016 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Sun Jan 01 2017 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Mon Jan 02 2017 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Tue Jan 31 2017 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Wed Feb 01 2017 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Thu Feb 02 2017 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Fri Feb 03 2017 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Sat Feb 04 2017 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Sun Feb 05 2017 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Mon Feb 06 2017 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Tue Feb 07 2017 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Wed Feb 08 2017 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Thu Feb 09 2017 01:00:00 GMT+0100 (Central European Standard Time)'),
new Date('Fri Feb 10 2017 01:00:00 GMT+0100 (Central European Standard Time)')
];
console.log(groupByMonth(dates));
.as-console-wrapper { top: 0; max-height: 100% !important; }

how to check time overlap in jquery or javascript

I have array-object like below in jquery
var subevent = [
{
start: Fri Jun 23 2017 15:15:00 GMT+0530 (India Standard Time),
end: Fri Jun 23 2017 14:15:00 GMT+0530 (India Standard Time)
},
{
start: Fri Jun 23 2017 15:15:00 GMT+0530 (India Standard Time),
end: Fri Jun 23 2017 16:15:00 GMT+0530 (India Standard Time)
},
{
start: Fri Jun 23 2017 16:15:00 GMT+0530 (India Standard Time),
end: Fri Jun 23 2017 15:15:00 GMT+0530 (India Standard Time)
},
.
.
.
]
How can I check if two timings are not overlapping?
Please pass unixtime in array like below example
var subevent = [
{
start: Mon Jun 26 2017 10:15:00 GMT+0530 (India Standard Time),
startunix: 1498452300000,
end: Mon Jun 26 2017 11:15:00 GMT+0530 (India Standard Time),
endunix: 1498455900000
},
{
start: Mon Jun 26 2017 11:15:00 GMT+0530 (India Standard Time),
startunix: 1498455900000,
end: Mon Jun 26 2017 12:15:00 GMT+0530 (India Standard Time),
endunix: 1498459500000
}
]
then sorting array in time ascending order
var sortedArray = subevent.sort(function(a,b){
return a.startunix - b.startunix;
});
use sorted array in for loop and store endunix time in one variable lastEndTime and check current iteration start time with lastEndTime variable like below code
var errorFlag = 0;
var lastEndTime;
for(var i=0; i<sortedArray.length; i++) {
var currentStartTime;
if( sortedArray[i].endunix <= sortedArray[i].startunix ){
alert('time slot conflict')
errorFlag = 1;
break;
}
if( !lastEndTime ) {
lastEndTime = sortedArray[i].endunix;
//console.log(" i where last end time not exists = "+i+" lastEndTime "+lastEndTime);
} else {
currentStartTime = sortedArray[i].startunix;
//console.log(" i where last end time exists = "+i+" currentStartTime "+currentStartTime+" lastEndTime "+lastEndTime );
if ( currentStartTime < lastEndTime ) {
alert('time overlapping')
errorFlag = 1;
break;
}
lastEndTime = sortedArray[i].endunix;
}
}
console.log(errorFlag);
there is also display alert or errorflag, if alert display or errorflag is one then something went wrong otherwise everything is ok.
Try with new Date().getTime()
In my answer
true means both are same time
Check the status of each object its show the overlap or not
var subevent = [{ start: "Fri Jun 23 2017 15:15:00 GMT+0530 (India Standard Time)", end: "Fri Jun 23 2017 14:15:00 GMT+0530 (India Standard Time)"},{ start: "Fri Jun 23 2017 15:15:00 GMT+0530 (India Standard Time)", end: "Fri Jun 23 2017 16:15:00 GMT+0530 (India Standard Time)"}, { start: "Fri Jun 23 2017 16:15:00 GMT+0530 (India Standard Time)", end: "Fri Jun 23 2017 15:15:00 GMT+0530 (India Standard Time)"},]
subevent.forEach(function(a) {
a.status = new Date(a.start).getTime() == new Date(a.end).getTime();
})
console.log(subevent)
Try this, it checks if the end date is before start date.
var subevent = [{
start: 'Fri Jun 23 2017 15:15:00 GMT+0530(India Standard Time)',
end: 'Fri Jun 23 2017 14:15:00 GMT+0530(India Standard Time)'
},
{
start: 'Fri Jun 23 2017 15:15:00 GMT+0530(India Standard Time)',
end: 'Fri Jun 23 2017 16:15:00 GMT+0530(India Standard Time)'
},
{
start: 'Fri Jun 23 2017 16:15:00 GMT+0530(India Standard Time)',
end: 'Fri Jun 23 2017 15:15:00 GMT+0530(India Standard Time)'
}
];
for (let event of subevent) {
let startTime = new Date(event.start).getTime(),
endTime = new Date(event.end).getTime();
event.isOverlapping = endTime <= startTime;
}
console.log(subevent);

date loop increment and growing variable ( mootools )

Can someone please take a look at this with fresh eyes.
var start_date = Date.parse('2013-07-01');
var i_date = Date.parse('2013-07-5');
console.log(start_date + '---before loop ');
for (var n = start_date; n < i_date; n.increment()) {
console.log(start_date + '---inside loop ');
}
console.log(start_date + '---after loop ');
This code produces this:
Mon Jul 01 2013 00:00:00 GMT+0200 (W. Europe Daylight Time)---before loop
Mon Jul 01 2013 00:00:00 GMT+0200 (W. Europe Daylight Time)---inside loop
Tue Jul 02 2013 00:00:00 GMT+0200 (W. Europe Daylight Time)---inside loop
Wed Jul 03 2013 00:00:00 GMT+0200 (W. Europe Daylight Time)---inside loop
Thu Jul 04 2013 00:00:00 GMT+0200 (W. Europe Daylight Time)---inside loop
Fri Jul 05 2013 00:00:00 GMT+0200 (W. Europe Daylight Time)---after loop
Why does start_date variable grow?
(fiddle here if needed)
The problem is that n and start_date are pointing to the same object. You need to clone the date by creating new Date object, for example:
n = new Date(start_date);
Updated demo.
Example:
> a = new Date()
Sun Jul 07 2013 19:51:09 GMT+0600 (Ekaterinburg Standard Time)
> b = a
Sun Jul 07 2013 19:51:09 GMT+0600 (Ekaterinburg Standard Time)
> c = new Date(a)
Sun Jul 07 2013 19:51:09 GMT+0600 (Ekaterinburg Standard Time)
// Do some stuff with "a"
> a
Sat Jun 29 2013 19:51:09 GMT+0600 (Ekaterinburg Standard Time)
> b
Sat Jun 29 2013 19:51:09 GMT+0600 (Ekaterinburg Standard Time)
> c
Sun Jul 07 2013 19:51:09 GMT+0600 (Ekaterinburg Standard Time)

Categories

Resources