I have a script that retrieves weeknumbers and years from an API and converts them to MomentJS date objects. Most of these entries have the correct date, only the last object goes wrong, and I can't see why.
This is the json-structure:
{
results: {
years: {
2013: {
weeks: [
40, 41, 42, 43
]
}
}
}
}
In Javascript I do the following loop:
for(var i in results.years[year].weeks) {
var week = parseInt(results.years[year].weeks[i]);
var startDate = '' + year + '-' + ( (week.toString().length == 1) ? '' + '0' + week : week ) + '-1';
var eindDate = '' + year + '-' + ( (week.toString().length == 1) ? '0' + week : week ) + '-7';
console.log(moment(startDate, 'GGGG-WW-E', true)); // returns the right date object
var startPeriode = moment(startDate, 'GGGG-WW-E');
console.log(startPeriode); //If week = 43, this returns a date object with date (01 october 2013), rest is all good
var eindPeriode = moment(eindDate, 'GGGG-W-E', true); //Object is always correct
}
The error currently only occurs when the week is 43 (is the last weeknumber retrieved from the API) and when calculating the startDate (eindDate is flawless currently). The moment object also contains some other variables, these look perfect, P.E.:
startDate._a = [
'2013',
9,
21,
0,
0,
0,
0
]
I've also tried the following:
moment('2013-W43-1');
moment('2013-43-1', 'GGGG-W-E');
And making use of some of the elements in the moment object:
var startPeriode = moment('2013-W43-1');
var startDay = new Date(startPeriode._a[0] + '-' + startPeriode._a[1] + '-' + startPeriode._a[2]);
var startPeriode = moment(startDay);
I really don't know how to fix this, and why this goes wrong. Anyone some ideas?