Why does New Date () always return null?

If the date format $scope.timestamp = '2016-12-16 07:02:15 am'

I want to format to 16/12/2016 07:02:15 am

I tried this code below and it works well

$scope.originalStamp = $filter('date')
                          (new Date($scope.timestamp.replace("-","/")),'dd-MM-yyyy HH:mm:ss a');

But my question is: why is it new Date($scope.timestamp)always returned nullif you do not use the replacement of char from (-) to (/)?

see below code does not work if am without using replace().

$scope.originalStamp = $filter('date')
                              (new Date($scope.timestamp),'dd-MM-yyyy HH:mm:ss a');

Why new Date()doesn't it accept if the date format is (-)? Is the new Date () conversion dependent on my system date format?

+4
source share
1 answer

Why doesn't the new Date () accept if the date format has (-)?

, ISO 8601, ECMA-262, . , . Date Date.parse ( ), .

Date() ?

. , (. MDN), , , -, , .

:

PS

'2016-12-16 07:02:15 am' '16/12/2016 07:02:15 am ', :

// Reformat a date like '2016-12-16 07:02:15 am' as
// '16/12/2016 07:02:15 am'
function reformatDate(s) {
  var b = s.split(/[ -]/);
  return [b[2],b[1],b[0]].join('/') + ' ' + b[3] + ' ' + b[4];
}
 
var s = '2016-12-16 07:02:15 am';
console.log(s + ' => ' + reformatDate(s))
Hide result
+5

Source: https://habr.com/ru/post/1666457/


All Articles