Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript日期对象与月的第一天有关_Javascript_Jquery_Html - Fatal编程技术网

Javascript日期对象与月的第一天有关

Javascript日期对象与月的第一天有关,javascript,jquery,html,Javascript,Jquery,Html,我正在使用Javascript日期,我对尝试从字符串中获取日期感到有点困惑 这是我的代码: var formatDate = function(dateObj) { // make sure date values are two digits and months start at 1 var adjMonth = dateObj.getMonth() + 1; var adjDate = dateObj.getDate(); if (adjMonth <

我正在使用Javascript日期,我对尝试从字符串中获取日期感到有点困惑

这是我的代码:

var formatDate = function(dateObj) {
    // make sure date values are two digits and months start at 1
    var adjMonth = dateObj.getMonth() + 1;
    var adjDate = dateObj.getDate();
    if (adjMonth < 10) adjMonth = '0' + adjMonth;
    if (adjDate < 10) adjDate = '0' + adjDate;

    // build and return dateStr
    var dateStr = dateObj.getFullYear() + '-' + adjMonth + '-' + adjDate;
    return dateStr;
};

$(document).ready(function() {
    var testIn1 = "2012-02-01";
    var testDate1 = new Date(testIn1);
    var testDate1Str = formatDate(testDate1);

    var testIn2 = "2012-01-31";
    var testDate2 = new Date(testIn2);
    var testDate2Str = formatDate(testDate2);

    $('#output').html("---Input = '" + testIn1 + "':<br>" + testDate1 + "<br>" + testDate1Str + "<br>"
                     +"---Input = '" + testIn2 + "':<br>" + testDate2 + "<br>" + testDate2Str + "<br>");
});​

这对我来说毫无意义,为什么休息一天?从2012年02月01日到2012年01月31日似乎并不明智。。。我错过了什么

它看起来像是
Date。parse
使用
00:00:00
GMT,如果您没有超过某个时间,它将是您所在时区(GMT-6)的前一天
18:00:00
。如果确实通过了显式时间,则此行为将被抑制:

Date.parse(testIn1 + " 00:00:00");

它看起来像是
Date。parse
使用
00:00:00
GMT,如果您没有通过时间,它将是您时区(GMT-6)的前一天
18:00:00
。如果确实通过了显式时间,则此行为将被抑制:

Date.parse(testIn1 + " 00:00:00");

嗯,我明白了。。。所以js假设我用GMT输入日期,然后用CST输出转换后的时间?有点像。它实际上也在以GMT表示时间,只是没有使用date对象中给出的
.getTimezoneOffset()
。嗯,我明白了。。。所以js假设我用GMT输入日期,然后用CST输出转换后的时间?有点像。它实际上也是以GMT表示时间,只是没有使用date对象中给定的
.getTimezoneOffset()