Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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_Date - Fatal编程技术网

Javascript从输入类型日期返回不正确的日期

Javascript从输入类型日期返回不正确的日期,javascript,date,Javascript,Date,当我选择一个日期(比如2014年3月12日)时,它会在我输入日期的前一天返回2014年12月2日星期二16:00:00 GMT-0800太平洋标准时间 <script> function getAge() { var today = new Date(); var birthDate = new Date(document.forms["name"]["birth"].value); var age = today.getFullYear() - birt

当我选择一个日期(比如2014年3月12日)时,它会在我输入日期的前一天返回2014年12月2日星期二16:00:00 GMT-0800太平洋标准时间

 <script>

function getAge() {
    var today = new Date();
    var birthDate = new Date(document.forms["name"]["birth"].value);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();

    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate()+1)) {
        age--;
    }
    document.getElementById('age').innerHTML = birthDate;
}
</script>

<form name='name' onsubmit='return false'>
  <input type="date" name="birth">
<p id='age'></p>
<button onclick='getAge()'>check</button>

</form>

不正确的日期是由于您的时区设置造成的。现在是格林尼治时间8点。
考虑使用UTC:

不正确的日期是由于时区设置。现在是格林尼治时间8点。


考虑使用UTC:

< P>这是一个老问题,但我想重启它,因为这是我的谷歌查询的第一个结果,而被接受的答案并不能真正回答这个问题。因此,我为其他人提供了更多的信息

Date对象中有几个方便的方法,这些方法可能对那些试图解决这个JavaScript小问题的人有所帮助

在我看来,似乎没有办法以编程方式更改日期对象时区偏移量,必须使用字符串操作来完成,这确实是不可取的,但没有其他方法。在解释日期时,日期对象构造函数始终使用本地计算机时区,这是无法更改的。因此,您必须了解,当您创建一个基本上忽略时区的日期时,修改后的日期对象将表示与原始日期不同的时间点。新的Date对象将自动采用本地计算机的时区,但它将使用您在下面两个选项的Date类的单个构造函数参数或字符串参数中指定的年、月、日期、小时、分钟、秒、毫秒

我找到了两种方法来解决这个问题,并从type=date输入字段生成正确的日期。请使用您认为对您的解决方案更有效或更合适的解决方案。就我个人而言,我更喜欢使用选项B,因为它比字符串操作更易于编程,而且可能更可靠。它也有点简单

选项A:UTC日期字符串操作

根据世界时间,返回日期对象的1到31之间的月份日期。UTC方法计算日期时假设日期对象为本地时间和日期

getUTCString-根据通用时间将日期对象转换为字符串

注意:日期构造函数恢复为本地时区。因此,您不能使用getUTCString值构建新日期并期望从输入字段中获取正确的日期

您可以通过使用getUTCDate并调用dateObject.setDatedateObject.getUTCDate;,来解决此问题;,它只更正对象的日期部分,因此您还需要使用dateObject.setMonthdateObject.getUTCMonth更正月份和年份;和dateObject.setFullYeardateObject.getUTCFullYear;分别地或者,您可以删除getUTCString值的GMT部分之后的所有内容,这将为您提供一个没有时区名称的字符串。然后可以使用该字符串创建一个新的日期对象,该对象将默认为客户端的时区

//convert to UTC to avoid "date minus one" issues where the supplied date is interpreted incorrectly as
//the previous date.
date = new Date(document.getElementById('date').value);
console.log("original date: " + date.toString());
console.log("UTC date string: " + date.toUTCString());
//produces local machine timezone, and therefore incorrect date value
date = new Date(date.toUTCString());
console.log("UTC date string Date constructor: " + date.toString());
utcDate = new Date(utcString.substr(0, utcString.indexOf("GMT")));
console.log("UTC date string Date constructor (without 'GMT*'): " + utcDate.toString());
console.log("UTC date number: " + date.getUTCDate());
date.setDate(date.getUTCDate());
date.setMonth(date.getUTCMonth());
date.setFullYear(date.getUTCFullYear());
console.log("UTC modified date: " + date.toString());
输出:

original date: Fri Jan 29 2021 23:16:11 GMT-0600 (Central Standard Time)
UTC date string: Sat, 30 Jan 2021 05:16:11 GMT
UTC date string Date constructor: Fri Jan 29 2021 23:16:11 GMT-0600 (Central Standard Time)
UTC date string Date constructor (without 'GMT*'): Sat Jan 30 2021 23:16:11 GMT-0600 (Central Standard Time)
UTC date number: 30
UTC modified date: Sat Jan 30 2021 23:16:11 GMT-0600 (Central Standard Time)
original date: Fri Jan 29 2021 23:04:03 GMT-0800 (Pacific Standard Time)
Date with UTC parameters: Sat Jan 30 2021 07:04:03 GMT-0800 (Pacific Standard Time)
选项B:提供迄今为止的所有UTC参数 另见:

例如:

date = new Date(document.getElementById('date').value);
console.log("original date: " + date.toString());
date = new Date(
    date.getUTCFullYear(),
    date.getUTCMonth(),
    date.getUTCDate(),
    date.getUTCHours(),
    date.getUTCMinutes(),
    date.getUTCSeconds(),
    date.getUTCMilliseconds());
console.log("Date with UTC parameters: " + date.toString());
输出:

original date: Fri Jan 29 2021 23:16:11 GMT-0600 (Central Standard Time)
UTC date string: Sat, 30 Jan 2021 05:16:11 GMT
UTC date string Date constructor: Fri Jan 29 2021 23:16:11 GMT-0600 (Central Standard Time)
UTC date string Date constructor (without 'GMT*'): Sat Jan 30 2021 23:16:11 GMT-0600 (Central Standard Time)
UTC date number: 30
UTC modified date: Sat Jan 30 2021 23:16:11 GMT-0600 (Central Standard Time)
original date: Fri Jan 29 2021 23:04:03 GMT-0800 (Pacific Standard Time)
Date with UTC parameters: Sat Jan 30 2021 07:04:03 GMT-0800 (Pacific Standard Time)

这是一个老问题,但我想重新提出它,因为这是我谷歌查询的第一个结果,而被接受的答案并不能很好地回答这个问题。因此,我为其他人提供了更多的信息

Date对象中有几个方便的方法,这些方法可能对那些试图解决这个JavaScript小问题的人有所帮助

在我看来,似乎没有办法以编程方式更改日期对象时区偏移量,必须使用字符串操作来完成,这确实是不可取的,但没有其他方法。在解释日期时,日期对象构造函数始终使用本地计算机时区,这是无法更改的。因此,您必须了解,当您创建一个基本上忽略时区的日期时,修改后的日期对象将表示与原始日期不同的时间点。新的Date对象将自动采用本地计算机的时区,但它将使用您在下面两个选项的Date类的单个构造函数参数或字符串参数中指定的年、月、日期、小时、分钟、秒、毫秒

我找到了两种方法来解决这个问题,并从type=date输入字段生成正确的日期。请使用您认为对您的解决方案更有效或更合适的解决方案。就我个人而言,我更喜欢使用选项B,因为它比字符串操作更易于编程,而且可能更可靠。它也有点简单

选项A:UTC日期字符串操作

根据世界时间,返回日期对象的1到31之间的月份日期。UTC方法计算日期时假设日期对象为本地时间和日期

getUTCString-根据通用时间将日期对象转换为字符串

注:日期为 rts返回本地时区。因此,您不能使用getUTCString值构建新日期并期望从输入字段中获取正确的日期

您可以通过使用getUTCDate并调用dateObject.setDatedateObject.getUTCDate;,来解决此问题;,它只更正对象的日期部分,因此您还需要使用dateObject.setMonthdateObject.getUTCMonth更正月份和年份;和dateObject.setFullYeardateObject.getUTCFullYear;分别地或者,您可以删除getUTCString值的GMT部分之后的所有内容,这将为您提供一个没有时区名称的字符串。然后可以使用该字符串创建一个新的日期对象,该对象将默认为客户端的时区

//convert to UTC to avoid "date minus one" issues where the supplied date is interpreted incorrectly as
//the previous date.
date = new Date(document.getElementById('date').value);
console.log("original date: " + date.toString());
console.log("UTC date string: " + date.toUTCString());
//produces local machine timezone, and therefore incorrect date value
date = new Date(date.toUTCString());
console.log("UTC date string Date constructor: " + date.toString());
utcDate = new Date(utcString.substr(0, utcString.indexOf("GMT")));
console.log("UTC date string Date constructor (without 'GMT*'): " + utcDate.toString());
console.log("UTC date number: " + date.getUTCDate());
date.setDate(date.getUTCDate());
date.setMonth(date.getUTCMonth());
date.setFullYear(date.getUTCFullYear());
console.log("UTC modified date: " + date.toString());
输出:

original date: Fri Jan 29 2021 23:16:11 GMT-0600 (Central Standard Time)
UTC date string: Sat, 30 Jan 2021 05:16:11 GMT
UTC date string Date constructor: Fri Jan 29 2021 23:16:11 GMT-0600 (Central Standard Time)
UTC date string Date constructor (without 'GMT*'): Sat Jan 30 2021 23:16:11 GMT-0600 (Central Standard Time)
UTC date number: 30
UTC modified date: Sat Jan 30 2021 23:16:11 GMT-0600 (Central Standard Time)
original date: Fri Jan 29 2021 23:04:03 GMT-0800 (Pacific Standard Time)
Date with UTC parameters: Sat Jan 30 2021 07:04:03 GMT-0800 (Pacific Standard Time)
选项B:提供迄今为止的所有UTC参数 另见:

例如:

date = new Date(document.getElementById('date').value);
console.log("original date: " + date.toString());
date = new Date(
    date.getUTCFullYear(),
    date.getUTCMonth(),
    date.getUTCDate(),
    date.getUTCHours(),
    date.getUTCMinutes(),
    date.getUTCSeconds(),
    date.getUTCMilliseconds());
console.log("Date with UTC parameters: " + date.toString());
输出:

original date: Fri Jan 29 2021 23:16:11 GMT-0600 (Central Standard Time)
UTC date string: Sat, 30 Jan 2021 05:16:11 GMT
UTC date string Date constructor: Fri Jan 29 2021 23:16:11 GMT-0600 (Central Standard Time)
UTC date string Date constructor (without 'GMT*'): Sat Jan 30 2021 23:16:11 GMT-0600 (Central Standard Time)
UTC date number: 30
UTC modified date: Sat Jan 30 2021 23:16:11 GMT-0600 (Central Standard Time)
original date: Fri Jan 29 2021 23:04:03 GMT-0800 (Pacific Standard Time)
Date with UTC parameters: Sat Jan 30 2021 07:04:03 GMT-0800 (Pacific Standard Time)

这是一个时区问题。我在南非,当我运行代码时,它会给我正确的选择日期。这是一个时区问题。我在南非,当我运行代码时,它会给我正确的选择日期。