"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Does IE Date Constructor Differ from Chrome and Firefox Date Handling?

Why Does IE Date Constructor Differ from Chrome and Firefox Date Handling?

Published on 2024-11-08
Browse:195

Why Does IE Date Constructor Differ from Chrome and Firefox Date Handling?

IE Date Constructor Issue: NaN vs. Functionality in Other Browsers

Within a project involving JavaScript calendar development, discrepancies emerged in date handling between Internet Explorer (IE) and browsers like Firefox and Chrome. Specifically, IE's date functions were resulting in NaN (Not a Number) values, while they functioned properly in the other browsers.

Upon investigation, it became clear that the root cause was related to the date format being used. The function in question, buildWeek(), intended to generate header dates for a calendar week based on a Monday date provided in the 'm, d, Y' format, for example, "02, 01, 2010". However, in IE, this format was not being recognized correctly, leading to the NaN issue.

To resolve this discrepancy, a different approach was adopted. Instead of relying on the given format, the date string was split into its components (date and time) using split(" "). Subsequently, the date component was further split into its constituent parts (year, month, day) using split("-"), and the time component was split into its parts (hours, minutes, seconds) using split(":").

An instance of Date was then constructed using these parsed values. This method proved to be compatible with all browsers, ensuring consistent date handling across IE, Firefox, and Chrome.

var dateStr = "2011-08-03 09:15:11"; //returned from mysql timestamp/datetime field
var a = dateStr.split(" ");
var d = a[0].split("-");
var t = a[1].split(":");
var date = new Date(d[0], (d[1] - 1), d[2], t[0], t[1], t[2]);

By adopting this approach, the NaN errors in IE were successfully eliminated, achieving consistent date handling across different browsers.

Release Statement This article is reprinted at: 1729402457 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3