2017-10-18 27 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 
} 
});