Javascript日期功能显示日期错误 [英] Javascript date function showing wrong date

查看:169
本文介绍了Javascript日期功能显示日期错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码显示了以下日期,月份和年份:

  6 
3
116

哪个明显是错误的日期。

  var date = new Date(); 
var day = date.getDay();
var month = date.getMonth();
var year = date.getYear();
console.log(day);
console.log(month);
console.log(year);
function next(){

};

小提琴

解决方案

getDay()返回



getMonth()从0- 11,so 3表示April,



getYear()已被弃用,替换为 getFullYear()应该使用它。



它只是看起来像所有这些功能都做的不同于你所期望的。 p>

要从1-31获取一个月的日期: getDate()



要获得月份像您期待的,只需添加1: getMonth()+ 1


The code below shows the day, month and year as:

6
3
116

Which is obviously wrong dates.

var date= new Date();
var day=date.getDay();
var month=date.getMonth();
var year=date.getYear();
    console.log( day );
    console.log( month );
    console.log( year );
    function next() {

    };

Fiddle.

解决方案

getDay() returns the day of the week from 0-6 for Sun-Sat,

getMonth() returns the month from 0-11, so 3 means April,

getYear() has been deprecated and replaced with getFullYear() which should be used instead.

It just looks like all of these functions do something different than what you were expecting.

To get day of the month from 1-31: getDate(),

To get month like you were expecting, just add 1: getMonth() + 1

这篇关于Javascript日期功能显示日期错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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