Tolga Yasar
Tolga Yasar

Reputation: 45

isValid() function from moment.js library returns false for something that should be true

const moment = require('moment');

var format_1 = 'dddd, MMMM DD, yyyy';
var date_1 = 'Thursday, January 03, 2019';
console.log(moment(date_1, format_1).isValid());// This returns true 


var format_2 = 'dddd, MMMM DD, yyyy';
var date_2 = 'Friday, May 01, 2020';
console.log(moment(date_2, format_2).isValid());// And this returns false

The first console.log() returns like true, as expected. Proving any date in the year 2020 the result of the isValid() function is false for this specific time format. Thanks for any help :)

Upvotes: 1

Views: 883

Answers (1)

Sanket Phansekar
Sanket Phansekar

Reputation: 741

Year is represented as YYYY as per moment.js docs

Modified your existing code to get the desirable output below :

var format_1 = 'dddd, MMMM DD, YYYY';
var date_1 = 'Thursday, January 03, 2019';
console.log(moment(date_1, format_1).isValid());// This returns true 

var format_2 = 'dddd, MMMM DD, YYYY';
var date_2 = 'Friday, May 01, 2020';
console.log(moment(date_2, format_2).isValid());// And this returns true as well
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>

Upvotes: 1

Related Questions