具有当前日期的元素的日期,并分配唯一ID
Date of element with current date and assign unique ID
我有一系列具有唯一日期的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>
我一直在努力确定当前日期是否介于每周潜水的开始日期和结束日期之间。
$('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和下周的div分配一个ID。
如有任何指导,我们将不胜感激。
以下内容应该有效。我认为checked
是您原始代码中的一个拼写错误。我还添加了一个更改背景颜色的示例。这可以很容易地更改为根据您的喜好设置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');
}
});
JSFiddle:http://jsfiddle.net/markwylde/4wuwZ/
顺便说一句,如果你想在这个项目中做很多与日期相关的任务,我会研究moment.js框架,或者类似的框架。这里有一篇很好的stackoverflow文章,解释了使用它可以完成上述任务的整洁方法:
我假设您使用服务器端语言输出HTML?如果是这样的话,我只需要在div中添加一个带有Unix时间的属性,并在此基础上运行比较器。我相信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>
然后:
$('div.dinnersWeeks').each( function() {
currentDate = Math.round(new Date().getTime() / 1000);
console.log(check > $(this).find('div.startingDate').attr('z-start-unix') && currentDate < $(this).find('div.endingDate').attr('z-end-unix'));
});
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 在循环中分配json值时,值被覆盖
- 如何为动态创建的元素分配唯一的id-Javascript
- 使用Javascript为Pyramid的每个块分配唯一的id
- 为动态创建的元素分配唯一名称
- 具有当前日期的元素的日期,并分配唯一ID
- 分配唯一的id和名称
- 使用全局变量为动态创建的元素分配唯一id和唯一名称
- 如何为一对克隆对象分配唯一的id和href
- 通过find()和each()迭代分配唯一的ID
- 如何分配或附加一个唯一的ID在javascript与行整数从html
- 为angular指令实例分配自定义的唯一id
- 分配,在主索引页上为脚本使用唯一的Wordpress帖子id
- 创建分配唯一项的算法
- 我想为每个用户分配一个唯一的URL-PHP
- 在Jquery中为类的所有元素分配唯一ID
- 如何为每个复选框分配一个唯一的id
- 如何使用jquery/javascript为html中的ID列生成和分配唯一编号
- 如何为以下方案的每个选择框分配唯一 ID
- 在选择时为单选按钮分配唯一id