在php中将您的日期写入您网页上的隐藏输入。以如下格式存储它:YYYY/MM/DD hh:mm:ss
。加载日期由隐藏字段的值传递给Date构造函数:
var cricketDateField = document.getElementsById("cricketDateField");
var cricketDate = new Date(cricketDateField.value);
获取当前的日期只是通过调用Date构造函数不带参数:
var now = new Date();
获取在毫秒之差减去日期:
var msDiff = cricketDate - now; // difference in milliseconds
然后,您可以手动解析毫秒,或转换成日期和提取日期的各部分获得的区别:
var diff = new Date(msDiff - 62167190400000); // difference as a date
var years = diff.getYear();
var months = diff.getMonth();
var days = diff.getDate() - 1;
var hours = diff.getHours();
var minutes = diff.getMinutes();
var seconds = diff.getSeconds();
var msg = "There are " + years + " years, " +
months + " months, " +
days + " days, " +
hours + " hours, " +
minutes + " minutes, and " +
seconds + " seconds until the cricket match.";
document.getElementById("differenceMsg").innerHTML = msg;