Javascript 具有当前日期并分配唯一ID的元素的日期

Javascript 具有当前日期并分配唯一ID的元素的日期,javascript,jquery,Javascript,Jquery,我有一系列具有唯一日期的div,包括与div内容相关的一周开始和一周结束。如下所示: <div class="dinnersWeeks"> <div class="startingDate">8/25/2013 12:00 AM</div> <div class="endingDate">8/31/2013 12:00 AM</div> <div class="weekLinkMenus"><a href=

我有一系列具有唯一日期的div,包括与div内容相关的一周开始和一周结束。如下所示:

<div class="dinnersWeeks">
  <div class="startingDate">8/25/2013 12:00 AM</div>
  <div class="endingDate">8/31/2013 12:00 AM</div>
  <div class="weekLinkMenus"><a href="testweek.aspx">testweek.aspx</a></div>
</div>
<div class="dinnersWeeks">
  <div class="startingDate">9/1/2013 12:00 AM</div>
  <div class="endingDate">9/7/2013 12:00 AM</div>
  <div class="weekLinkMenus"><a href="generalweek11.aspx">generalweek11.aspx</a></div>
</div>
<div class="dinnersWeeks">
  <div class="startingDate">9/8/2013 12:00 AM</div>
  <div class="endingDate">9/14/2013 12:00 AM</div>
  <div class="weekLinkMenus"><a href="generalweek12.aspx">generalweek12.aspx</a></div>
</div>

2013年8月25日上午12:00
2013年8月31日上午12:00
2013年1月9日上午12:00
2013年9月7日上午12:00
2013年9月8日上午12:00
2013年9月14日上午12:00
我一直在努力确定当前日期是否介于每周div的开始日期和结束日期之间

$('div.dinnersWeeks').each( function() {
    sd = new Date( $(this).find( 'div.startingDate' ).text() );
    ed = new Date( $(this).find( 'div.endingDate' ).text() );
    currentDate = new Date();
    console.log(check > sd && currentDate < ed);
});
$('div.dintersweeks')。每个(函数(){
sd=新日期($(this).find('div.startingDate').text());
ed=新日期($(this).find('div.endingDate').text());
currentDate=新日期();
日志(检查>sd&¤tDate
之后,我将为本周的div和下周的div分配一个ID


非常感谢您的指导。

我想您是用服务器端语言输出HTML的吧?如果是这样的话,我只需要在Unix时间内为div添加一个属性,并在此基础上运行比较器。我确信jQuery中有一个库可以进行日期比较,但是如果您不想做任何复杂的事情,可以使用Unix

<div class="dinnersWeeks">
  <div class="startingDate" z-start-unix="3000392">8/25/2013 12:00 AM</div>
  <div class="endingDate" z-end-unix="3001394">8/31/2013 12:00 AM</div>
  <div class="weekLinkMenus"><a href="testweek.aspx">testweek.aspx</a></div>
</div>

2013年8月25日上午12:00
2013年8月31日上午12:00
然后:

$('div.dintersweeks')。每个(函数(){
currentDate=Math.round(new Date().getTime()/1000);
console.log(检查>$(此).find('div.startingDate').attr('z-start-unix')和¤tDate<$(此).find('div.endingDate').attr('z-end-unix'));
});

以下各项应能起作用。我认为选中的
是您原始代码中的一个输入错误。我还添加了一个更改背景颜色的示例。这可以很容易地更改,以根据您的偏好设置
id

JavaScript

$('div.dinnersWeeks').each( function() {
    sd = new Date( $(this).find( 'div.startingDate' ).text() );
    ed = new Date( $(this).find( 'div.endingDate' ).text() );
    currentDate = new Date();
    if(currentDate > sd && currentDate < ed) {
        $(this).css('background-color', 'red'); 
    }
});
$('div.dintersweeks')。每个(函数(){
sd=新日期($(this).find('div.startingDate').text());
ed=新日期($(this).find('div.endingDate').text());
currentDate=新日期();
如果(currentDate>sd&¤tDate
JSFiddle


顺便说一句,如果你想在这个项目中完成很多与日期相关的任务,我会研究一下moment.js框架,或者类似的框架。这里有一篇很好的帖子,解释了一种简洁的方法,你可以用它来做上面的事情:

你会反对使用库的解决方案吗?check来自哪里:?
console.log(currentDate>sd&¤tDate
应该可以工作。你和OP有同样的问题:未定义的变量
check
+1 for。上述方法在Chrome、Firefox和IE10中都适用,但浏览器对从字符串解析日期的支持往往不一致。
$('div.dinnersWeeks').each( function() {
    sd = new Date( $(this).find( 'div.startingDate' ).text() );
    ed = new Date( $(this).find( 'div.endingDate' ).text() );
    currentDate = new Date();
    if(currentDate > sd && currentDate < ed) {
        $(this).css('background-color', 'red'); 
    }
});