简体   繁体   English

从现在开始动态显示日期2天

[英]Displaying Date 2 Days From Now Dynamically on Page

I'm trying to set up the Date +2 days from now a few times on my page. 我正尝试从现在开始几次设置日期+2天。

So here is the code that I'm using: 所以这是我正在使用的代码:

<span class="spanDate"></span><br> at 8pm EST</div>

var dayName = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday'];

var months = ['January','February','March','April','May','June','July', 'August','September','October','November','December'];

var tomorrow = new Date();
tomorrow.setTime(tomorrow.getTime() + (1000*3600*48));

document.getElementsByClassName("spanDate").innerHTML = dayName[tomorrow.getDay()] + ", " + months[tomorrow.getMonth()] + " " + tomorrow.getDate()+ ", " + tomorrow.getFullYear();
</script>`

Questions: 问题:

  1. why doesn't it work for me? 为什么对我不起作用?

  2. should I use document.getElementsByID instead? 我应该改用document.getElementsByID吗? (I want to use the span multiple times on the page) (我想在页面上多次使用span

Thanks for your help!! 谢谢你的帮助!!

Use this: 用这个:

var dayName = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday'];
var months = ['January','February','March','April','May','June','July', 'August','September','October','November','December'];

var tomorrow = new Date();
tomorrow.setTime(tomorrow.getTime() + (1000*3600*48));
var dt = dayName[tomorrow.getDay()] + ", " + months[tomorrow.getMonth()] + " " + tomorrow.getDate()+ ", " + tomorrow.getFullYear();
//add [0]
document.getElementsByClassName('spanDate')[0].innerHTML = dt;

because this: 因为这:

document.getElementsByClassName("spanDate").innerHTML

returns a nodeList not an element. 返回一个nodeList而不是一个元素。

EDIT: 编辑:

If you have 5 span elements in your document where you want to show this date you can do it the classic way: 如果您的文档中有5个span元素要显示此日期,则可以采用经典方式:

document.getElementsByClassName('spanDate')[0].innerHTML = dt;
document.getElementsByClassName('spanDate')[1].innerHTML = dt;
document.getElementsByClassName('spanDate')[2].innerHTML = dt;
document.getElementsByClassName('spanDate')[3].innerHTML = dt;
document.getElementsByClassName('spanDate')[4].innerHTML = dt;

or use a loop of any kind that will iterate through the nodes like this example: 或使用将遍历节点的任何类型的循环(如本例所示):

var e = document.getElementsByClassName('spanDate');
for(i=0; i < e.length; i++) {
    e[i].innerHTML = dt;
}

Cheers! 干杯!

If you were to use Moment.js, you could "simplify" your code as follows: 如果要使用Moment.js,可以按以下方式“简化”代码:

var el = document.getElementsByClassName("spanDate")[0],
    tomorrow = moment().add(2, "d");

el.innerHTML = tomorrow.format("DD, MM, YYYY");

You'll notice that I added [0] after the getElementsByClassName("spanDate") , that's because the method getElementsByClassName returns an HTML collection ( array-like structure ). 您会注意到我在getElementsByClassName("spanDate")之后添加了[0] ,这是因为getElementsByClassName方法返回了HTML集合( 类似数组的结构 )。 Once you have that collection, you need to extract the element that you want to modify ( in this case the first one we found [0] ). 一旦有了该集合,就需要提取要修改的元素( 在这种情况下,我们找到的第一个元素[0] )。

要回答第二个问题:最好不要在同一页面中多次使用相同的id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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