Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Moment.js
Message
 
 
À
13/05/2017 04:11:32
Information générale
Forum:
Javascript
Catégorie:
Autre
Titre:
Divers
Thread ID:
01651078
Message ID:
01652014
Vues:
44
>>Hi everybody,
>>
>>I'm getting the age as NaN displayed and also I see the following in the console:
>>
>>Birth date changed - old Value: 2010-05-01T00:00:00 new Value: 2010-05-01T00:00:00 age: NaN
>>
>>The code is the following:
>>
>>
>> $scope.$watch("currentData.birthDate", function (newValue, oldValue) {
>>                    
>>                    if (newValue) {                        
>>                        $scope.currentData.age = moment(new Date(), "MM/DD/YYYY").diff(moment(newValue, "MM/DD/YYYY"), "years");
>>                        $log.debug("Birth date changed - old Value: " + oldValue + " new Value: " + newValue + " age: " + $scope.currentData.age);
>>                    }
>>                    
>>                });
>>
>>Do you see what is wrong here and why the age is calculated wrongly? This is only when the page is loaded the first time and not when the date is being changed in the interface.
>
>Where did you get that syntax from? There is no moment function that takes a date and a string - it's either a date OR a string (with an optional second string to inform on the format of the date in the first string). IAC all you need is :
$scope.currentData.age = moment().diff(newValue,"years");
Read the docs: https://momentjs.com/docs/


I'm getting the deprecation warning. My current code is
$scope.$watch("currentData.birthDate", function (newValue, oldValue) {
                    $log.debug("$watch currentData.birthDate: newValue = " + newValue + " oldValue = " + oldValue);
                    if (newValue) {                        
                        $scope.currentData.age = moment().diff(newValue, "years");                       
                    }
                    
                });
Originally the newValue and oldValue both set to the actual birth date and it is in ISO format.

When I get the date from the calendar, the newValue comes in the MM/DD/YYYY format and the oldValue is undefined.

How should I change the code above so it would work correctly for both cases and don't show me the warning?

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform