1

現在の日付を前の日付と次の日付のリンクで表示する単純な Javascript に取り組んでいます。

次のように私のコードスニペット

<script type="text/javascript">
var currentTime = new Date();

var month = currentTime.getMonth()+1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = month + " " + day + " " + year;

function ShowSchedule() 
{
document.getElementById("today").innerHTML = date;
}

function ShowDay(isNextDay)
{
if(isNextDay)
{
var nextDay = new Date(year, month, day+1);
date = nextDay.getMonth() + " " + nextDay.getDate() + " " + nextDay.getFullYear();
ShowSchedule();
}
else{
var prevDay = new Date(year, month, day -1);
date= prevDay.getMonth() + " " + prevDay.getDate() + " " + prevDay.getFullYear();
ShowSchedule();
}
}

</script>
</head>
<body>
<table border="1">
    <tr>
        <th>
            <a id = "prev" href="#" onClick="ShowDay(false)">Prev</a>
        </th>
        <th id="today">
        </th>
        <th>
            <a id = "next" href="#" onClick="ShowDay(true)">Next</a>
        </th>
    </tr>
</table>
<script>ShowSchedule();</script>
</body>
</html>

上記のコードは、現在、前の日付、および次の日付でも機能しますが、問題は、翌日のリンクをクリックすると、1 日だけ増加することです。前のリンクも同じです。私の要件は、前と次のリンクナビゲーションをクリックすることで、好きな日付に移動できることです。

どんな助けでも大歓迎です。

4

2 に答える 2

2
new Date((new Date).valueOf() + 86350989) //That will always give you tomorrow (jump ahead 24 hours)

new Date((new Date).valueOf() - 86350989) //Will give you yesterday.

何が起こっているのか:

new Date(number) //You're specifying a DateTime object using the milliseconds from unix epoch (January 1, 1970)

(new Date).valueOf() //You're getting the milliseconds from unix epoch.

86350989 //Is the number of milliseconds in a day.
于 2012-05-07T17:17:05.823 に答える