How to convert string to number for logical operators in javascript?

154 views Asked by At

I have date-picker where i am setting validation for start and end date , so end date should always be greater than start date if its not throw error... I am seeing the issue converting string to number in comparison..

So far tried code

main.html

<div class="form-group col-md-6 fieldHeight">
    <label for="cycleStart" class="col-md-4 required">Cycle Start</label>
    <div class="col-md-8">
        <input kendo-date-picker
        type="text" class="form-control" id="cycleStart"
        name="cycleStart"
        ng-model="rcsaCycleDTO.cycStartDate" 
        k-ng-model="cycleStrtObj"
        onkeydown="return false;"
        ng-change="validateDate"
        required data-required-msg="Cycle start is required">
        <p class="text-danger" ng-show="createRcsaCycleForm.cycleStart.$touched && createRcsaCycleForm.cycleStart.$error.required">Cycle Start is required field</p>                                
    </div>
<div class="form-group col-md-6 fieldHeight">
    <label for="cycleEnd" class="col-md-4 required">Cycle end (due date)</label>
    <div class="col-md-8">
        <input 
        kendo-date-picker
        type="text" class="form-control" id="cycleEnd"
        name="cycleEnd"
        ng-model="rcsaCycleDTO.cycEndDate"
        k-min="cycleStrtObj" 
        k-rebind="cycleStrtObj"
        onkeydown="return false;"
        ng-change="validateDate"
        required>
        <p class="text-danger" ng-show="createRcsaCycleForm.cycleEnd.$touched && createRcsaCycleForm.cycleEnd.$error.required">Cycle End is required field</p>
        <p class="text-danger" ng-show="createRcsaCycleForm.cycleEnd.$error.dateRange"> Cycle due date can not be less than cycle start date</p>
    </div>
</div>

main.js

$scope.validateDate = function(){
          var startDate = Date.parse($scope.rcsaCycleDTO.cycStartDate);
          var endDate =  Date.parse($scope.rcsaCycleDTO.cycEndDate);
          if(startDate > endDate) {
            $scope.createRcsaCycleForm.cycleEnd.$setValidity('dateRange',false);
          } else {
            $scope.createRcsaCycleForm.cycleEnd.$setValidity('dateRange',true);
          }
        }
0

There are 0 answers