Javascript日期方法不一致 - getDate vs getMonth [英] Javascript Date method inconsistency - getDate vs getMonth

查看:122
本文介绍了Javascript日期方法不一致 - getDate vs getMonth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这导致我昨晚头痛,我想明白为什么Date对象中的getDate方法为1(返回值为1-31),而getMonth方法为0(返回0-11) 。我想知道为什么同一个对象的方法不一致。



我明白为什么很难改变现在的行为,但是有这样的理由是这样设计的这在第一位?



文档可以在这里找到: http ://www.w3schools.com/jsref/jsref_obj_date.asp

解决方案

我想几个月是0因为 Java做了同样的事情方式 JavaScript语言设计时。



编辑 Oracle取代了较旧的Java文档,有一个该页面的归档版本


This caused me a bit of a headache last night and I wanted to understand why the getDate method in the Date object is 1 based (returns values from 1-31) while the getMonth method is 0 based (returns 0-11). I'm wondering why there is this inconsistency in methods for the same object.

I understand why it's difficult to change the behavior now but are there any reasons this was designed like this in the first place?

Documentation can be found here: http://www.w3schools.com/jsref/jsref_obj_date.asp

解决方案

I suppose months are 0-based because Java did it the same way when the JavaScript language was designed.

EDIT Oracle took down older Java documentation, there is an archived version of that page.

这篇关于Javascript日期方法不一致 - getDate vs getMonth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆