gpt4 book ai didi

javascript - 呈现日期的差异 - IE JavaScript 与 Chrome JavaScript

转载 作者:行者123 更新时间:2023-11-30 07:40:45 27 4
gpt4 key购买 nike

我有以下 JavaScript 代码片段,用于呈现从当前月份到去年同月的日期范围:

var today = new Date();
var endDate = new Date(today.getFullYear(), today.getMonth(), 1);
var startDate = new Date(endDate.getYear() - 1, endDate.getMonth() - 1, 1);

在 IE 8 中它给了我正确的日期范围:

Fri Jun 1 00:00:00 MST 2012 - Mon Jul 1 00:00:00 MST 2013

当我在 Chrome 中运行相同的代码时,我得到以下日期范围:

Wed Jun 01 0112 00:00:00 MST - Mon Jul 01 2013 00:00:00 MST

开始日期的年份是 0112。我需要做什么才能在 IE 和 Chrome 中获得正确的日期范围?

最佳答案

What do I need to do in order to get the correct date range in IE and Chrome?

endDatestartDate 使用 getFullYear():

var startDate = new Date(endDate.getFullYear() - 1, endDate.getMonth() - 1, 1);

getYear() 旨在返回短年份 -- 96 表示 1996。但是,对于 1900 年代以外年份的日期,其行为因浏览器而异。

具体来说,IE 4 - 8模仿其他年份的 getFullYear():

In Internet Explorer 4.0 through Internet Explorer 8 standards mode, the formula depends on the year. For the years 1900 through 1999, the value returned is a 2-digit value that is the stored year minus 1900. For dates outside that range, the 4-digit year is returned. For example, 1996 is returned as 96, but 1825 and 2025 are returned as is.

IE9返回standardized behavior您在 Chrome 中看到的(伪代码):

getYear() == getFullYear() - 1900

关于javascript - 呈现日期的差异 - IE JavaScript 与 Chrome JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17795970/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com