How to use split in angularjs

im get the date for such a ng model

Thu May 21, 2015 18:47:07 GMT + 0700 (SE Asia Standard Time)

but he shows me in the console "TypeError: date.split is not a function"how to fix it?

   $scope.test = function(date) {
    console.log(date);
    $scope.d = (date.split(' ')[0]);
    $scope.m = (date.split(' ')[1]);
    $scope.y = (date.split(' ')[2]);
    $scope.dd = (date.split(' ')[3]);

}
+4
source share
2 answers

The problem is that dateit is not String, it is a Date instance of the object. Therefore, you cannot use split on it. To use it, you can first convert it to String. For example, like him:

$scope.test = function(date) {
    date = date.toString();
    $scope.d = (date.split(' ')[0]);
    $scope.m = (date.split(' ')[1]);
    $scope.y = (date.split(' ')[2]);
    $scope.dd = (date.split(' ')[3]);    
};

When consolidating a journal, console.log(date);it automatically calls toString, so for you it looks like a string.

, split . Date, getMonth, .getFullYear ..

+4

, , , , Javascript Date object, split(). , :

$scope.d = (date.toString().split(' ')[0]);

, , , . :

$scope.d = date.getDate();
$scope.m = date.getMonth() + 1;
$scope.y = date.getFullYear();

..

. , , , , , :

<div class="myDate">Year: {{date.getFullYear()}}
0

All Articles