I've encountered a strange issue while working on an overview page. I need to display the maximum date from loaded data to provide users with the time period they're looking at. Even though I already know what date it should be (either today or yesterday, updated nightly), I still want to use a max function as a failsafe in case of any update errors.
The problem arises when both d3.max and my custom max-date function return an incorrect date - one month ahead. For example, if today's date is supposed to be 7/9/2015, it displays as 7/10/2015 instead. This results in an empty array when trying to filter data for 7/10. Interestingly, this code works fine in a fiddle, so there might be some underlying issue. Could it have something to do with the d3.tsv (even though the file is actually tab-separated despite the .csv extension)? Any thoughts on where I could be going wrong? The parse function itself returns correct results, with dates formatted as dd.mm.yyyy.
//Dateformatting
function parseDate(dateStr) {
var s1 = dateStr.split(" ");
var s1dat = s1[0].split(".");
return new Date(s1dat[2], s1dat[1], s1dat[0]);
};
var dateArr = [];
//Occupation
d3.tsv("data.csv", function(error, data, tsv) {
datasetIn = data;
datasetIn.forEach(function(d) {
d.datum = parseDate(d.datum);
d.Patients = +d.Patients.replace(",", ".");
d.Beds= +d.Antal_vardplats.replace(",", ".");
});
for (index = 0; index < datasetIn.length; ++index) {
dateArr.push(datasetIn[index].datum);
}
var maxYear = d3.max(dateArr).getFullYear();
var maxDate = d3.max(dateArr);
})