Reputation: 4520
I'm trying to use moment to check if the date/time presented is valid. I'm interested in validating dates in the following format:
10/10/2016 20:45
I've tried using this code, but the date is always considered invalid:
moment("10/10/2016 20:45", "dd/MM/YYYY HH:mm", true);
Any tips on what I'm doing wrong?
thanks.
Luis
Upvotes: 0
Views: 966
Reputation: 31482
You have 2045
in the input string, while you have HH:mm
in the format, the problem is the :
, change the input to 20:45
or the format to HHmm
.
Moreover the token for day is the uppercase DD
instead of the lowercase dd
, see docs here.
Here a working example:
var m1 = moment("10/10/2016 2045", "dd/MM/YYYY HH:mm", true);
var m2 = moment("10/10/2016 2045", "DD/MM/YYYY HHmm", true);
var m3 = moment("10/10/2016 20:45", "DD/MM/YYYY HH:mm", true);
console.log(m1.isValid()); // false
console.log(m2.isValid()); // true
console.log(m3.isValid()); // true
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Upvotes: 2