What's wrong with this javascript sort - javascript

I'm not getting the sort results I expect. What's wrong.
Here's the code:
data.sort(function(a,b){
return (Date.parse(a) - Date.parse(b));
});
for(var j = 0; j < data.length; j++){
var item = data[j];
console.log(j+") " +item.createdAt+":"+Date.parse(item.createdAt));
}
And here's the output:
0) Fri Jun 10 2016 16:58:26 GMT-0400 (Eastern Daylight Time):1465592306000
1) Tue Jun 07 2016 08:07:34 GMT-0400 (Eastern Daylight Time):1465301254000
2) Fri Jun 10 2016 15:57:44 GMT-0400 (Eastern Daylight Time):1465588664000
3) Fri Jun 10 2016 14:34:45 GMT-0400 (Eastern Daylight Time):1465583685000
4) Fri Jun 10 2016 14:22:02 GMT-0400 (Eastern Daylight Time):1465582922000
5) Fri Jun 10 2016 11:14:34 GMT-0400 (Eastern Daylight Time):1465571674000
6) Fri Jun 10 2016 10:03:56 GMT-0400 (Eastern Daylight Time):1465567436000
7) Fri Jun 10 2016 10:02:58 GMT-0400 (Eastern Daylight Time):1465567378000
8) Thu Jun 09 2016 16:32:29 GMT-0400 (Eastern Daylight Time):1465504349000
9) Thu Jun 09 2016 16:29:24 GMT-0400 (Eastern Daylight Time):1465504164000

As data is array of object and each object contains the createdAt property on which the array should be sorted, you need
data.sort(function(a, b) {
return (Date.parse(a.createdAt) - Date.parse(b.createdAt));
});
If the data is coming from server database, I'll suggest to get the data sorted on the date from the Database itself.

Tushar hit it on the head, if that is what you're looking to sort on. If you want to go further in depth about sorting on a particular value comparison you'll want to research the array prototype. You can find that here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

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);
}
}

How to populate weeks number based on date occurance in 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);

javascript - date difference should be zero but it is 18 hours

This one has stumped me. It should be so simple I would think. I am doing some very simple date subtraction in Javascript. I am subtracting the same dates and I would think it would give zero hours, but it gives 18 hours.
let inDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime();
let outDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime();
document.getElementById('date').innerHTML = new Date(outDate - inDate);
<div id='date'>
</div>
In case it produces different results based on where you are, the result I am getting is this:
Wed Dec 31 1969 18:00:00 GMT-0600 (Central Standard Time)
This is due to your timezone. If you convert to GMT String before print it the time will be correct. (Jan 01, 1969 00:00:00)
new Date(outDate - inDate).toGMTString()
You should see the correct date.
let inDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime()
let outDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime()
console.log(new Date(inDate - outDate).toGMTString())

Javascript Array unshift new Date subtracting 1 Day

I am getting some strange results from the following code:
a = [];
a[0] = new Date();
console.log("1 Element Added: "+a.length + " - " + a.toString());
//"1 Element Added: 1 - Sun Jun 02 2019 12:13:35 GMT-0400 (Eastern Daylight Time)"
a.unshift(new Date(new Date(new Date().setDate(a[0].getDate() - 1))));
console.log("First Unshift: "+a.length + " - " + a.toString());
//"First Unshift: 2 - Sat Jun 01 2019 12:13:35 GMT-0400 (Eastern Daylight Time),Sun Jun 02 2019 12:13:35 GMT-0400 (Eastern Daylight Time)"
a.unshift(new Date(new Date(new Date().setDate(a[0].getDate() - 1))));
console.log("Second Unshift: "+a.length + " - " + a.toString());
//"Second Unshift: 3 - Fri May 31 2019 12:13:35 GMT-0400 (Eastern Daylight Time),Sat Jun 01 2019 12:13:35 GMT-0400 (Eastern Daylight Time),Sun Jun 02 2019 12:13:35 GMT-0400 (Eastern Daylight Time)"
a.unshift(new Date(new Date(new Date().setDate(a[0].getDate() - 1))));
console.log("Third Unshift: "+a.length + " - " + a.toString());
//"Third Unshift: 4 - Sun Jun 30 2019 12:13:35 GMT-0400 (Eastern Daylight Time),Fri May 31 2019 12:13:35 GMT-0400 (Eastern Daylight Time),Sat Jun 01 2019 12:13:35 GMT-0400 (Eastern Daylight Time),Sun Jun 02 2019 12:13:35 GMT-0400 (Eastern Daylight Time)"
Same code works 1st and 2nd time, but the 3rd run gives an unexpected result - it should be Thu May 30 2019, not Jun 30 2019 Can someone tell me what I'm doing wrong here?
Thanks in Advance.
The innermost new Date() always makes a Date instance in June. When you set the day-of-month to 30, you're forcing the date to June 30th, not May 30th.
Calling .setDate() can change the month, but only when the day-of-month is something that doesn't make sense, either smaller (zero or negative) or bigger (like 33). Since 30 is indeed a real day in June, the month doesn't change.
#Pointy and #Titus have already explained why the code does not work as you expected. Here I leave your code modified to react as you wanted:
a = [];
a[0] = new Date();
console.log("1 Element Added: "+a.length + " - " + a.toString());
//"1 Element Added: 1 - Sun Jun 02 2019 12:13:35 GMT-0400 (Eastern Daylight Time)"
a.unshift(new Date(a[0]));
a[0].setDate(a[0].getDate()-1);
console.log("First Unshift: "+a.length + " - " + a.toString());
//"First Unshift: 2 - Sat Jun 01 2019 12:13:35 GMT-0400 (Eastern Daylight Time),Sun Jun 02 2019 12:13:35 GMT-0400 (Eastern Daylight Time)"
a.unshift(new Date(a[0]));
a[0].setDate(a[0].getDate()-1);
console.log("Second Unshift: "+a.length + " - " + a.toString());
//"Second Unshift: 3 - Fri May 31 2019 12:13:35 GMT-0400 (Eastern Daylight Time),Sat Jun 01 2019 12:13:35 GMT-0400 (Eastern Daylight Time),Sun Jun 02 2019 12:13:35 GMT-0400 (Eastern Daylight Time)"
a.unshift(new Date(a[0]));
a[0].setDate(a[0].getDate()-1);
console.log("Third Unshift: "+a.length + " - " + a.toString());

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