简体   繁体   中英

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:

    <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. What am I doing wrong?

Second date will be same(lastvisit) for every td so no problem

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. .after() will add the after the your last span inside td

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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