0

I have enter invalid data such as "09/31/2016 10:10 AM"

but actually we dont have any 31th in september 2016, What date.parse() is doing that its calculating the next date "Oct 01, 2016 @ 10:10 AM" as output.

angular.module("myModule", [])
       .controller("myController", ['$scope', function ($scope) {
                    
           $scope.dDate = "09/31/2016 10:10 AM";
                    
           $scope.format = function () {
               $scope.display = Date.parse($scope.dDate);
           }
           $scope.format(); // invoke the format()
       }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myModule" >
    <div ng-controller="myController" ng-form="frm">
        <input type="text" name="name" style="width:250px;" ng-model="dDate" ng-change="format()" placeholder="MM/dd/YYYY hh:mm AM/PM" ng-pattern="/^(0[1-9]|1[012])[/]([123]0|[012][1-9]|31)[/](19[0-9]{2}|2[0-9]{3}) ([01][0-9]|2[0-3]):([0-5][0-9]) (AM|PM|am|pm)$/" />
        <div ng-show="!frm.name.$error.pattern">
            {{display | date : 'MMM dd, yyyy @ hh:mm a'}}
        </div>
        <div>Error : {{frm.name.$error}}</div>
    </div>
</body>

have I done anything wrong?

RJV
  • 287
  • 1
  • 7
  • 20

2 Answers2

2

You haven’t done anything wrong, but you have to write your own date validator if you want to check for rollover:

function checkDate(str) {
    var matches = str.match(/(\d{1,2})[- \/](\d{1,2})[- \/](\d{4})/);
    if (!matches) return;

    // parse each piece and see if it makes a valid date object
    var month = parseInt(matches[1], 10);
    var day = parseInt(matches[2], 10);
    var year = parseInt(matches[3], 10);
    var date = new Date(year, month - 1, day);
    if (!date || !date.getTime()) return;

    // make sure we have no funny rollovers that the date object sometimes accepts
    // month > 12, day > what's allowed for the month
    if (date.getMonth() + 1 != month ||
        date.getFullYear() != year ||
        date.getDate() != day) {
            return;
        }
    return(date);
}

You can see this post from which the code was liberally borrowed: Is this a good way to check for a valid date in JavaScript?

Community
  • 1
  • 1
2ps
  • 15,099
  • 2
  • 27
  • 47
1

For validation of date, month or year or any of the combination of these. You can use moment.js library which is perfect for date time manipulation.

Use isValid() of moment library

Please refer http://momentjs.com/docs/

Manish Singh
  • 518
  • 3
  • 13
  • `moment("09/31/2016 10:10 AM").isValid()` is `true`. – Amadan Oct 20 '16 at 05:21
  • @Amadan that's because moment doesn't understad this format on it's own. check `moment('9/31/2016', 'MM/DD/YYYY').isValid()` is `false` – tanmay Oct 20 '16 at 05:31
  • 1
    Quoting [momentjs docs](http://momentjs.com/docs/#/parsing), "For consistent results parsing anything other than [ISO 8601](https://xkcd.com/1179/) strings, you should use String + Format." – tanmay Oct 20 '16 at 05:38
  • @tanmay: Very good point; if that was in the answer, I would have upvoted it. – Amadan Oct 20 '16 at 06:22