简体   繁体   English

在javascript / jquery中以不同格式比较两个日期

[英]Compare two dates in different formats in javascript/jquery

I'm trying to compare two dates in different formats to highlight new items added from last visit but there is one problem with first date: 我正在尝试以不同的格式比较两个日期,以突出显示上次访问时添加的新项目,但是第一个日期存在一个问题:

jQuery( "td.details > span:last-child" ).each(function() {
  var a = jQuery( this ).text();
  var b = a.split('/');
  var c = b[1]+'/'+e[0]+'/'+e[2]; //dd.mm.yyyy=>mm.dd.yyyy
  var d = new Date( c );
  var date = d.getTime();

    jQuery('<span/>', {
    'class':'date',
    'text': date,
    }) .appendTo( "td.details > p" );
});

HTML: HTML:

    <td class="details"><p>new item1</p><span>10/1/2017 17:06</span></td>
    <td class="details"><p>new item2</p><span>09/1/2017 22:26</span></td>
    <td class="details"><p>new item3</p><span>09/1/2017 11:18</span></td>
    <td class="details"><p>new item4</p><span>08/1/2017 14:32</span></td>

Every td have different date, expectation is in every td will be created span with appropriate d.getTime() value (just testing), but newly created span contains all d.getTime() values (from every td ) not only that one. 每个td具有不同的日期,期望在每个td中将使用适当的d.getTime()值创建span (只是测试),但是新创建的span包含所有d.getTime() values (来自每个td )。 What am I doing wrong? 我究竟做错了什么?

Second date will be same(lastvisit) for every td so no problem 第二期将在相同(lastvisit)为每一个td所以没问题

Will this work better? 这样会更好吗?

jQuery('<span/>', {
  'class':'date',
  'text': date,
}).after(this);

In your code you are appending date to every td.details > p not only the current one. 在您的代码中,您将date附加到每个td.details > p而不只是当前date .after() will add the after the your last span inside td .after()将在td的最后一个范围之后添加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM