I have date string like this 1 May 2010 To 15 Aug 2016
.I just want to check this as date string on my function and not to proceed on a function.When I'm checking this string with new Date(value)
its returning Invalid date.How can I check this as date?
Asked
Active
Viewed 2,682 times
2

Mohamed Nizar
- 780
- 9
- 30
-
2There is two dates? 1 May 2010 and 15 Aug 2016. You should split those and check both. – Evus Aug 25 '16 at 03:53
-
inspiration http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – daremachine Aug 25 '16 at 03:55
-
there are two dates and are these dates given by you or (static) or from some where, like dynamic – caldera.sac Aug 25 '16 at 03:56
4 Answers
3
I would use js date library moment js for this.
var checkDate = function (str){
var dates = str.split("To");
var flag = true;
for(var i = 0; i < dates.length; i++) {
if (moment(dates[i]).isValid()) {
flag = false;
}
}
return flag;
}
I have created a fiddle for that

Rafi Ud Daula Refat
- 2,187
- 19
- 28
-
I had change your function and I do prefer use it in my code.Thanks and see my answer. – Mohamed Nizar Aug 25 '16 at 04:20
-
-
Please do not do that. Read and follow [*the documentation*](http://momentjs.com/docs/#/parsing/string/) and **always** supply the format of the string to parse. Otherwise, you are leaving it up to the parser to guess. It will return the first attempt that returns a valid date, not necessarily the "correct" one. – RobG Aug 25 '16 at 05:02
1
var str = "1 May 2010 To 15 Aug 2016";
//First split string
var array = str.split("To");
//then check if both are dates
for(var i = 0; i < array.length; i++) {
if(isNaN(new Date(array[i]))) { //Checking date
alert(array[i]+ ' is not valid date');
}
}

Evus
- 400
- 4
- 12
1
When parsing strings you should always provide the format of the string to parse. This can be done very simply using the ES5 every method:
var isValid = '1 May 2010 To 15 Aug 2016'.split(' To ').every(function(s) {
return moment(s,'D MMM YYYY').isValid();
});
console.log(isValid);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.js"></script>
If arrow functions are OK, then:
var isValid = '1 May 2010 To 15 Aug 2016'.split(' To ').every(s=>moment(s,'D MMM YYYY').isValid());
does the job.

RobG
- 142,382
- 31
- 172
- 209
0
var checkDate = function (str){
var dates = str.split("To");
for(var i = 0; i < dates.length; i++) {
if (moment(dates[i]).isValid()) {
return true;
} else {
return false;
}
}
}
I can use this function in my code anywhere.

Mohamed Nizar
- 780
- 9
- 30
-
i have changed my function according to your need. you need to do it in that way. please check. here your code does not have the chance to check seccond part of the array. example: think at the case "1 May 2001 To 15 Aug 20a6". it will return true. but the second part is not valid. Check my function and if it is ok mark it as accepted as it would help others. – Rafi Ud Daula Refat Aug 25 '16 at 04:30
-
You can replace the entire function with `str.split(' To ').every(s=>return moment(s).isValid());` but you should provide the string format to the parser. – RobG Aug 25 '16 at 06:00