Debojyoti Debojyoti - 1 year ago 71
HTML Question

Cannot compare present and form date in javascript (angular form)

In html,the input date tag is given like this:

<input type="date" placeholder="Select You Date of Birth" ng-model="dob">

In controller I have written the code like this:

$scope.dateOfBirth = new Date(dob).valueOf();
$scope.presentDate = new Date().valueOf();
if($scope.presentDate <= $scope.dateOfBirth){
content: 'Date of birth should be before current date'

I have the set the condition that the date of birth(dob) should be less then present date other wise the ionic warning popup will appear.But when i give the current date as dob, the ionic warning popup is not showing.Though when the dob is greater than the present date,the ionic popup is working correctly.

I have added a fiddle for testing.
Thanks for your time.

Fiddle link:

Answer Source

use format date yyyymmdd

instead of new Date().valueOf();

like this :

function TodoCtrl($scope) {
  $scope.dobSubmit = function(dob){
    $scope.dateOfBirth = new Date(dob);
    $scope.presentDate = new Date();
    if(formatDateYmd($scope.presentDate) > formatDateYmd($scope.dateOfBirth)){
      alert('present date is greater than date of birth which is correct format');
      alert('date of birth is greater or equal to present date which is incorrect format');

  var formatDateYmd = function(date) {
    var day = date.getDate();
    var monthIndex = date.getMonth() + 1;
    var year = date.getFullYear();
    console.log(''+year+ (monthIndex < 10 ? '0' + monthIndex : monthIndex) + (day < 10 ? '0' + day : day));
    return ''+year+ (monthIndex < 10 ? '0' + monthIndex : monthIndex) + (day < 10 ? '0' + day : day);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download