I would like to make a UL with previous dates, the current date, and have these listed items nested within it. An example is this:
我希望使用之前的日期,当前日期制作UL,并将这些列出的项目嵌套在其中。一个例子是:
• Monday November 13, 2017
o List item
o List item
o List item
• Tuesday November 14, 2017
o List item
o List item
o List item
• Wednesday November 15, 2017
o List item
o List item
o List item
I would be getting the current date via JavaScript.
我将通过JavaScript获取当前日期。
How would I be able to do the following?
我怎么能做到以下几点?
- Get the current date via JavaScript?
- Store the previous dates (i.e. yesterday's date, the day before's date, etc.)?;
通过JavaScript获取当前日期?
存储以前的日期(即昨天的日期,前一天的日期等)?
note: every time a new list item is added (like a to do app), the current date will be saved. If i were to come back to my app the next day, yesterday's date is saved, and the current date is added. Hope that makes sense!
注意:每次添加新的列表项(如应用程序)时,都会保存当前日期。如果我第二天回到我的应用程序,保存昨天的日期,并添加当前日期。希望有道理!
Thanks in advance!
提前致谢!
2 个解决方案
#1
0
Apply following code.
应用以下代码。
var todayDate = new Date();
var yesterdayDate = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
var beforeDate = new Date(new Date().getTime() - 2 * ( 24 * 60 * 60 * 1000));
console.log("Today Date: " + todayDate );
console.log("yesterday Date: " + yesterdayDate );
console.log("before Date: " + beforeDate );
#2
0
Have a look at this example:
看看这个例子:
var ul = document.getElementById('mylist');
var daysAmount = 3;
var items = [
{v: 'v1'},
{v: 'v2'},
{v: 'v3'},
];
var ts = new Date() / 1;
for(var i = daysAmount; i >= 0; i--){ // 3 previous + today = 4 days on list
var shift = i * (1000 * 60 * 60 * 24);
var previousDay = new Date(ts - shift);
var li = document.createElement('li');
var nestedUl = document.createElement('ul');
items.forEach(function(el){
var nestedLi = document.createElement('li');
nestedLi.innerText = el.v;
nestedUl.appendChild(nestedLi);
});
li.innerText = previousDay;
li.appendChild(nestedUl);
ul.appendChild(li)
}
<ul id="mylist">
</ul>
#1
0
Apply following code.
应用以下代码。
var todayDate = new Date();
var yesterdayDate = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
var beforeDate = new Date(new Date().getTime() - 2 * ( 24 * 60 * 60 * 1000));
console.log("Today Date: " + todayDate );
console.log("yesterday Date: " + yesterdayDate );
console.log("before Date: " + beforeDate );
#2
0
Have a look at this example:
看看这个例子:
var ul = document.getElementById('mylist');
var daysAmount = 3;
var items = [
{v: 'v1'},
{v: 'v2'},
{v: 'v3'},
];
var ts = new Date() / 1;
for(var i = daysAmount; i >= 0; i--){ // 3 previous + today = 4 days on list
var shift = i * (1000 * 60 * 60 * 24);
var previousDay = new Date(ts - shift);
var li = document.createElement('li');
var nestedUl = document.createElement('ul');
items.forEach(function(el){
var nestedLi = document.createElement('li');
nestedLi.innerText = el.v;
nestedUl.appendChild(nestedLi);
});
li.innerText = previousDay;
li.appendChild(nestedUl);
ul.appendChild(li)
}
<ul id="mylist">
</ul>