0

Hi I would to like to compare string example 15/01/2017 with newDate()

var dates = jQuery('tr.Entries').find('td.event-date > a').map(function() { //event date format is e.g 15/01/2017
         return jQuery(this).text();
      }).get();
var currentDate = new Date();
jQuery.each(dates, function (index, value) {
console.log(value);
//var parts = value.split('/');
//var mydate = new Date(parts[2],parts[0]-1,parts[1]); 
//console.log("mydate is: "+mydate); 
if(value < currentDate){
    //do something
}
});
roshambo
  • 2,624
  • 6
  • 31
  • 54
  • Your question is unclear. What are you trying to achieve? what is the issue with current code? – Rajesh Oct 18 '17 at 10:27
  • Possible duplicate of [compare string with todays date in javascript](https://stackoverflow.com/questions/15063670/compare-string-with-todays-date-in-javascript) – Pete Oct 18 '17 at 10:29

3 Answers3

1

You just need to convert the current date to the same date format with which you are comparing.

var currentDate = new Date();
currentDate = ("0"+currentDate.getDate()).slice(-2) + "/" + ("0"+(currentDate.getMonth() + 1)).slice(-2) + "/" + currentDate.getFullYear();

Now your comparison with other values in dates should work fine.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • Thanks, easy solution! – roshambo Oct 18 '17 at 11:01
  • I have added a new question to the do something part now - need to remove a table row if value < currentDate https://stackoverflow.com/questions/46809327/remove-table-row-if-mapped-td-equals-something/46809903#46809903 thanks – roshambo Oct 18 '17 at 12:26
0

Why you use less than condition inside if statement simply do this

var dates = jQuery('tr.Entries').find('td.event-date > a').map(function() { //event date format is e.g 15/01/2017
     return jQuery(this).text();
  }).get();
  var currentDate = new Date();
 jQuery.each(dates, function (index, value) {
 console.log(value);



 var istrue = new Date();
 currentDate = ("0"+currentDate.getDate()).slice(-2) + "/" + ("0"+
(currentDate.getMonth() + 1)).slice(-2) + "/" + 
currentDate.getFullYear()=="15/01/2017";
 if(istrue){
//do something
 }
 });
-1

Although there are vanilla-javascript and Jquery-only based solutions, if your project is big enough I'd advice you to add moment.js to your project and use it for such comparisons. It will make your life easier.

Check it out on the moment.js website

DreamWave
  • 1,934
  • 3
  • 28
  • 59