如何更改特定ID的日期

时间:2016-09-14 11:21:14

标签: javascript

我想创建一个脚本,根据实际日期更改日/月/年。

我有3个id。

<span id="date1">September 14, 2016</span>

<span id="date2">September 13, 2016</span>

<span id="date3">September 12, 2016</span>

我想每天更改日期,例如date1 =实际日期,date2 =昨天,date3 =昨天之前。

这是我提出的一些JavaScript,但它并不像我需要的那样工作。

function datums(datuminsh) {
                var months = new Array(12);
                months[0] = "January";
                months[1] = "February";
                months[2] = "March";
                months[3] = "April";
                months[4] = "May";
                months[5] = "June";
                months[6] = "July";
                months[7] = "August";
                months[8] = "September";
                months[9] = "October";
                months[10] = "November";
                months[11] = "December";

                var current_date = new Date();
                current_date.setDate(current_date.getDate() - datuminsh);
                month_value = current_date.getMonth();
                day_value = current_date.getDate();
                year_value = current_date.getFullYear();

                document.write(months[month_value] + " " + day_value + ", " + year_value);
                }   

document.getElementById("date1").innerHTML = datums(0);
document.getElementById("date2").innerHTML = datums(1);
document.getElementById("date3").innerHTML = datums(2);

1 个答案:

答案 0 :(得分:0)

这很简单。昨天的日期只是今天的日期而不是一个。前天是今天的日期减2。

var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
var beforeYesterday = new Date();
beforeYesterday.setDate(beforeYesterday.getDate() - 2);

您唯一需要做的就是每天运行此代码以显示实际数据。有一个节点包:https://www.npmjs.com/package/node-schedule

使用此代码段:

var j = schedule.scheduleJob({hour: 00, minute: 00}, function(){
    walk.on('dir', function (dir, stat) {
        uploadDir.push(dir);
    });
});