-1

I created this code.

var selectedDate = new Date();
selectedDate = new Date(measure.date + " " + measure.column[key].time);
console.log(measure.date + " " + measure.column[key].time); //08/05/2017 08:05 <- dd/MM/yyyy HH:mm
console.log(selectedDate); //Sat Aug 05 2017 08:05:00 GMT+0200 (Romance Summer Time)
$scope.popupData.push({selectedHour: selectedDate, minHour:$scope.minvalueHour, maxHour:$scope.maxvalueHour, valueItem:measure.column[key].value});
console.log($scope.popupData[key].selectedHour);
//0: {selectedHour: undefined, minHour: "08:00", maxHour: "08:59", valueItem: "10", $$hashKey: "object:887"}length: 1__proto__: Array(0)

This code has two problems, the first, it changes the months for the days and the second although it has created the date shows the variable as undefined, I have indicated in the comments the result of the console.log

Bibi
  • 33
  • 1
  • 1
  • 10
  • What is `measure`? – Harsh Jaswal May 11 '18 at 10:59
  • {turnHours: "08:00", date: "08/05/2017", column: Array(1), $$hashKey: "object:90"} $$hashKey : "object:90" column : Array(1) 0 : time : "08:05" value : "10" __proto__ : Object length : 1 __proto__ : Array(0) date : "08/05/2017" turnHours : "08:00" __proto__ : Object – Bibi May 11 '18 at 11:01
  • something you might be missing it works fine here https://jsfiddle.net/5yh74ar4/ – super cool May 11 '18 at 11:49
  • In your example, the variable is a string and not a date format. (var selectedDate ='08/05/2017';) – Bibi May 11 '18 at 12:03
  • https://jsfiddle.net/supercool/320033Lv/ check this it doesn't matter . – super cool May 11 '18 at 12:32

1 Answers1

-1

The first issue changes the months for the days is caused by the fact that the new Date() constructor expects yyyy-MM-dd (ISO 8601 format) and doesn't do such a great job with other inputs.

Fix:

// Parse measure.date into ISO 8601
var parsedMeasureDate = measure.date.split("/").reverse().join('-');
var selectedDate = new Date(parsedMeasureDate);

// Parse time into array and manually set hours and minutes
var parsedMeasureTime = measure.column[key].time.split(":");
selectedDate.setHours(parsedMeasureTime[0]);
selectedDate.setMinutes(parsedMeasureTime[1]);

As for the second issue, that's odd. Try creating the object beforehand, logging it and then pushing it.

var x = {
    selectedHour: selectedDate, 
    minHour:$scope.minvalueHour, 
    maxHour:$scope.maxvalueHour, 
    valueItem:measure.column[key].value
};

console.log(x);

$scope.popupData.push(x);

Also, as a tip, you should try to always store a Date object and use the date angular filter to display in whatever format you want.

Protozoid
  • 1,207
  • 1
  • 8
  • 16
  • Thanks this solved the first problem, I have discovered that the problem of not showing the value is in this line of html ---------> – Bibi May 11 '18 at 15:14
  • Nice, would you mind marking this as preferred answer then? :) – Protozoid May 11 '18 at 15:23
  • It makes no sense to parse a string to its components then create another string for the built-in parser to parse. Once you have the parts, pass them directly to the Date constructor. Also, ISO format dates are parsed as UTC, not local, which is inconsistent with ISO 8601 and likely unexpected as it creates a date for the previous day for timezones west of Greenwich. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG May 11 '18 at 20:52