为什么这段代码返回明天的日期?
它必须返回 2013-08-31,而不是 2013-09-01,因为我们是 8 月 31 日。
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_toisostring http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_toisostring
function myFunction() {
var d = new Date();
var x = document.getElementById("demo");
x.innerHTML = d.toISOString();
}
<p id="demo">Click the button to display the date and time as a string, using the ISO
standard.</p>
<button onclick="myFunction()">Try it</button>
是 UTC 时间。
如果您想获取当地时区,您必须自己格式化日期(使用getYear()
getMonth()
等)或使用一些库,例如date.js http://www.datejs.com/这将为您格式化日期。
使用 date.js 非常简单:
(new Date()).format('yyyy-MM-dd')
edit
正如 @MattJohnson 指出的 date.js 已被放弃,但您可以使用替代方案,例如moment.js http://momentjs.com/.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)