意外的javascript日期行为 [英] unexpected javascript date behavior
本文介绍了意外的javascript日期行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
var newDate=new Date('05/22/2012');
var month=newDate.getMonth();
var day=newDate.getDate()+(-2);
var year=newDate.getYear();
document.write(month+'/'+day+'/'+year);
我预计它会返回'05 / 20/2012',但它会返回'04 / 20 / 2012'
I expected it to return '05/20/2012' but instead it returns '04/20/2012'
这对我没有意义 - 有人可以帮我理解发生了什么以及如何得到正确答案吗?
This makes no sense to me - can someone help me understand what's going on and how to get the correct response?
感谢您的关注!
推荐答案
.getMonth()
从零开始。如 0 = 1月
和 11 = 12月
尝试
var month=newDate.getMonth() + 1;
这篇关于意外的javascript日期行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文