简体   繁体   English

JavaScript中使用getMonth()和getYear()的日期格式不正确

[英]Incorrect Date Format in JavaScript using getMonth() and getYear()

I am having trouble understanding why a Date in Javascript come out wrong. 我无法理解为什么Java Date中的Date出错。 For example... 例如...

$scope.campaign.date_start = new Date(19/11/2014);
Wed Nov 19 2014 00:00:00 GMT+0000 (GMT) - correct
$scope.campaign.date_start.getDay() = 19 - correct

So far so good, however getting the Month and the year gives me incorrect values.... 到目前为止一切顺利,但是获取月份和年份给我的值不正确。

$scope.campaign.date_start.getMonth() = 10 - incorrect should be 11
$scope.campaign.date_start.getYear() = 114 incorrect should be 2014

What I'm I going wrong here? 我在这里错了吗?

getMonth is 0 based, so you need to +1 to get the number of the current month. getMonth基于0,因此您需要+1以获取当前月份的编号。

getYear is deprecated, and may break at some point in future, it (generally) returns the number of years since 1900. The correct method to use is getFullYear getYear已弃用,并且将来可能会折断,它(通常)返回自1900年以来的年数。使用的正确方法是getFullYear

JavaScript getMonth() will always return actual month-1 . JavaScript getMonth()将始终返回实际的month-1 For the year, you will need to use getFullYear() to get the 4 digit year 对于年份,您将需要使用getFullYear()来获取4位数的年份

Date.getMonth() is 0-base (January is month 0). Date.getMonth()是从0开始的基数(一月是月份0)。

Date.getYear() is deprecated and is years since 1900 (hence 114). 不建议使用Date.getYear() ,它是1900年以来的年份(因此为114)。

http://www.w3schools.com/jsref/jsref_obj_date.asp http://www.w3schools.com/jsref/jsref_obj_date.asp

getMonth() is zero-based. getMonth()从零开始。 So you will need getMonth()+1 所以你需要getMonth()+1

getYear() was not Y2K compliant, so a new function getFullYear() was created to return 4-digit years. getYear()不兼容2000年版本,因此创建了一个新函数getFullYear()以返回4位数字的年份。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM