You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AngularJS日期转换map函数在IE浏览器中失效问题求助

Why Your Date Conversion Fails in IE (And How to Fix It)

Hey there! Let's break down why your code works everywhere except IE, and fix it step by step.

The Two Main Issues with IE

  1. No support for ES6 arrow functions
    IE (even IE11) doesn't recognize arrow functions like obj => { ... } that you're using in the map callback. This will throw a syntax error right away in IE, stopping your code dead in its tracks.

  2. Date string parsing inconsistencies
    IE's Date constructor is far pickier about date string formats than modern browsers. If your created_at value is an ISO 8601 string (like 2024-05-20T14:30:00), IE often fails to parse it correctly—even if you fix the arrow function issue.

Fixed Code for IE Compatibility

Here's your updated code that works across all browsers, including IE:

$http.get('/api/url').then(function(response){
    $scope.reports = response.data;
    $scope.reports = $scope.reports.map(function(obj) {
        // Preprocess the date string to play nice with IE
        let dateStr = obj.created_at;
        
        // Handle ISO 8601 format (replace 'T' with space, remove 'Z' if present)
        if (dateStr.includes('T')) {
            dateStr = dateStr.replace('T', ' ');
            if (dateStr.endsWith('Z')) {
                dateStr = dateStr.slice(0, -1);
            }
        }
        
        obj.created_at = new Date(dateStr);
        return obj;
    });
}, function(error){
    console.log("error");
});

Key Changes Explained

  • Replaced arrow function with a regular function: function(obj) { ... } is supported in all IE versions that can run AngularJS (IE9+).
  • Preprocessed the date string: By replacing the T separator with a space and stripping the optional Z timezone suffix, we give IE a date format it can reliably parse.

If your created_at uses a different format, you might need to adjust the preprocessing step, but this covers the most common ISO 8601 case that trips up IE.

内容的提问来源于stack exchange,提问作者My testing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:40:53