2017-10-18 24 views
0

您好我会喜欢到比较newDate()字符串例如15/01/2017比较字符串DD/MM/YYYY用的currentdate格式JS

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 
} 
}); 
+0

你的问题还不清楚。你想达到什么目的?当前代码有什么问题? – Rajesh

+0

[与javascript中的今天日期比较字符串]可能的重复(https://stackoverflow.com/questions/15063670/compare-string-with-todays-date-in-javascript) – Pete

回答

1

你只需要当前日期转换为与你是比较相同的日期格式。现在

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

您与其他值dates比较应该可以正常工作。

+0

谢谢,简单的解决方案! – roshambo

+0

我已经添加了一个新的问题,现在做某件事 - 如果值 roshambo

-1

虽然有香草javascript和jQuery的只有基础的解决方案,如果你的项目足够大我建议您将moment.js添加到您的项目中,并将其用于此类比较。 它会让你的生活更轻松。

检查出来的moment.js website

0

为什么你用不到里面如果语句条件简单地做到这一点

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 
} 
});