首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每次在对象中遇到开始日期和结束日期之间时,都会将div元素添加到页面中

每次在对象中遇到开始日期和结束日期之间时,都会将div元素添加到页面中
EN

Stack Overflow用户
提问于 2020-04-18 22:26:43
回答 1查看 38关注 0票数 0

我正在尝试在页面中为数组中的每个对象创建div元素,其中todays日期在开始日期和结束日期参数之间,非常感谢任何帮助。其思想是使用存储在数组中的每个事件的摘要信息作为对象,为每个相应的日期创建和显示事件框。

代码语言:javascript
复制
var $todaysdate = new Date();
var $fullyear = $todaysdate.getFullYear();
var $month = $todaysdate.getMonth();
var $day = $todaysdate.getDate();
var $finaltoday = new Date($fullyear, $month, $day).toDateString();

var eventyz = [{
  startDate: new Date("2020-4-15").toDateString(),
  endDate: new Date("2020-4-27").toDateString(),
  summary: "Info 1"
}, {
  startDate: new Date("2020-4-28").toDateString(),
  endDate: new Date("2020-4-28").toDateString(),
  summary: "Info 2"
}, {
  startDate: new Date("2020-4-28").toDateString(),
  endDate: new Date("2020-4-28").toDateString(),
  summary: "Info 3"
}, {
  startDate: new Date("2020-5-4").toDateString(),
  endDate: new Date("2020-5-4").toDateString(),
  summary: "Info4"
}, {
  startDate: new Date("2020-5-4").toDateString(),
  endDate: new Date("2020-5-4").toDateString(),
  summary: "Info5"
}, {
  startDate: new Date("2020-5-4").toDateString(),
  endDate: new Date("2020-5-4").toDateString(),
  summary: "Info6"
}];

eventyz.forEach(function MyDumbFunction(item) {
  if (Date.parse($finaltoday) <= Date.parse(item.startDate) && Date.parse($finaltoday) >=  
    Date.parse(item.endDate)) {
    $(".tttt").append("<div class='red_block'></div>");
  }
});
代码语言:javascript
复制
.jigle {
  width: 300px;
  background: red;
  height: 100px;
}

.red_block {
  width: 300px;
  height: 300px;
  background-color: red;
}
代码语言:javascript
复制
<link href="https://fonts.googleapis.com/icon?family=Material+Icons|Material+Icons+Outlined" rel="stylesheet">
<h1 id="kk"></h1>
<div class="tttt"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-18 22:59:21

你的比较是倒退的。您做了小于开始范围和大于结束范围的操作。

代码语言:javascript
复制
var $todaysdate = new Date();
var $fullyear = $todaysdate.getFullYear();
var $month = $todaysdate.getMonth();
var $day = $todaysdate.getDate();
var $finaltoday = new Date($fullyear, $month, $day).toDateString();

var eventyz = [{
  startDate: new Date("2020-4-15").toDateString(),
  endDate: new Date("2020-4-27").toDateString(),
  summary: "Info 1"
}, {
  startDate: new Date("2020-4-28").toDateString(),
  endDate: new Date("2020-4-28").toDateString(),
  summary: "Info 2"
}, {
  startDate: new Date("2020-4-28").toDateString(),
  endDate: new Date("2020-4-28").toDateString(),
  summary: "Info 3"
}, {
  startDate: new Date("2020-5-4").toDateString(),
  endDate: new Date("2020-5-4").toDateString(),
  summary: "Info4"
}, {
  startDate: new Date("2020-5-4").toDateString(),
  endDate: new Date("2020-5-4").toDateString(),
  summary: "Info5"
}, {
  startDate: new Date("2020-5-4").toDateString(),
  endDate: new Date("2020-5-4").toDateString(),
  summary: "Info6"
}];

eventyz.forEach(function MyDumbFunction(item) {
  if (Date.parse($finaltoday) >= Date.parse(item.startDate) && Date.parse($finaltoday) <=  
    Date.parse(item.endDate)) {
    $(".tttt").append("<div class='red_block'></div>");
  }
});
代码语言:javascript
复制
.jigle {
  width: 300px;
  background: red;
  height: 100px;
}

.red_block {
  width: 300px;
  height: 300px;
  background-color: red;
}
代码语言:javascript
复制
<link href="https://fonts.googleapis.com/icon?family=Material+Icons|Material+Icons+Outlined" rel="stylesheet">
<h1 id="kk"></h1>
<div class="tttt"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61290748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档