Javascript Array unshift new Date subtracting 1 Day - javascript

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

Related

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())

Why is my array being overwritten?

I am using node.js for a project and I am trying to add certain dates to an array. However, when I do, it overwrites the entire array with the current date being added. Source below:
let startDate = new Date(2014, 0, 8, 19, 0, 0)
let endDate = new Date(2014, 0, 11, 19, 0, 0)
let datesToDownload = []
let datesInDB = [new Date(2014, 0, 8, 19, 0, 0), new Date(2014, 0, 9, 19, 0, 0), new Date(2014, 0, 10, 19, 0, 0), new Date(2014, 0, 8, 11, 0, 0)]
for (let i = startDate; i <= endDate; i.setDate(i.getDate() + 1)) {
console.log('CHECKING DATE: ' + i.toDateString())
if (!(datesInDB2.indexOf(i.toDateString()) >= 0)) {
console.log('NEW RECORD FOUND FOR: ' + i.toDateString())
datesToDownload.push(i)
console.log('i: ' + i)
for (let j in datesToDownload) {
console.log('element ' + j + ': ' + datesToDownload[j])
}
}
}
And the output looks like this:
CHECKING DATE: Wed Jan 08 2014
NEW RECORD FOUND FOR: Wed Jan 03 2014
i: Wed Jan 08 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
element 0: Wed Jan 08 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
CHECKING DATE: Thur Jan 09 2016
NEW RECORD FOUND FOR: Thur Jan 09 2016
i: Thur Jan 09 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
element 0: Thur Jan 09 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
element 1: Thur Jan 09 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
CHECKING DATE: Fri Jan 10 2016
NEW RECORD FOUND FOR: Fri Jan 10 2016
i: Fri Jan 10 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
element 0: Fri Jan 10 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
element 1: Fri Jan 10 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
element 2: Fri Jan 10 2016 20:00:00 GMT-0400 (Eastern Daylight Time)
......
Notice how the elements are all changed to the current date being inserted, though an element is being added.
Why is this happening? How can I prevent it?
You're pushing exactly the same Date instance into the array on each iteration. You can create a copy of it however:
datesToDownload.push(new Date(i));
A date is an object, and is passed by reference. In your for loop you are changing the date:
for (let i = startDate; i <= endDate; i.setDate(i.getDate() + 1)) {

What's wrong with this javascript sort

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

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