【问题标题】:Compare string dd/mm/yyyy with currentDate format js比较字符串 dd/mm/yyyy 与 currentDate 格式 js
【发布时间】:2017-10-18 10:24:54
【问题描述】:

您好,我想将字符串示例 15/01/2017 与 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
}
});

【问题讨论】:

标签: javascript jquery date


【解决方案1】:

您只需要将当前日期转换为您要比较的相同日期格式。

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

现在您与 dates 中的其他值的比较应该可以正常工作了。

【讨论】:

  • 谢谢,简单的解决方案!
  • 我现在在做某事部分添加了一个新问题 - 如果值 stackoverflow.com/questions/46809327/… 谢谢,需要删除表格行
【解决方案2】:

为什么在 if 语句中使用小于条件只是这样做

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

【讨论】:

    【解决方案3】:

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

    查看moment.js website

    【讨论】:

      猜你喜欢
      • 2013-07-04
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多