简体   繁体   English

AngularJS自定义过滤器无法在iOS和IE上运行

[英]AngularJS custom filter not working on iOS and IE

I have created the following filter to convert MySQL dates and adjust time for the timezone relatively to UTC. 我创建了以下过滤器,以转换MySQL日期并相对于UTC调整时区的时间。

angular.module('HIS')
    .filter('dateToISO', function () {
        return function (input) {
            var offset = new Date().getTimezoneOffset();
            var date = new Date(input);
            date.setTime(date.getTime()-offset*60000);
            return date.toISOString();
        };
    });

Then, I use the filter to convert dates to my preferred format and show them inside HTML as follows. 然后,我使用过滤器将日期转换为我的首选格式,并按如下所示在HTML中显示它们。 (I have changed the Angular interpolation tags to [[ ]] to avoid conflicts with Laravel's blade syntax {{ }} ) (为了避免与Laravel的blade语法{{ }}冲突,我将Angular插值标签更改为[[ ]]

[[prescription.patient.first_name]] [[prescription.patient.last_name]]<br>
[[prescription.created_at | dateToISO | date:"EEEE, d/M/yy h:mm a"]]

This works fine with all the desktop browsers except for IE . IE之外,此功能在所有桌面浏览器中均能正常工作 Also this do not work for browsers in iOS (Safari/Chrome). 同样,这不适用于iOS (Safari / Chrome)中的浏览器。

Working on desktop browsers except IE 在IE以外的桌面浏览器上工作

在IE以外的桌面浏览器上的工作示例

Not working on iOS browsers and IE. 在iOS浏览器和IE上不起作用。 The raw angular code is shown instead. 而是显示原始角度代码。

在iOS浏览器上不起作用

Important : 重要事项

When I was searching, I found that the problem with IE was solved in Angular v1.3.3 and above. 在搜索时,我发现Angular v1.3.3及更高版本中IE的问题已解决。 But I'm using v1.5.5 and still the problem is there. 但是我正在使用v1.5.5 ,但问题仍然存在。 There was no clue on the Internet about this situation on iOS browsers. 互联网上没有关于iOS浏览器上这种情况的线索。 Can anyone explain why this happen and how to solve this? 谁能解释为什么会这样以及如何解决呢?

Thanks in advance! 提前致谢!

I finally found the cause to the problem. 我终于找到了问题的原因。 I had used var date = new Date(input); 我用过var date = new Date(input); where input is in the format Ymd H:i:s which is a MySQL timestamp. 输入的格式为Ymd H:i:s ,这是MySQL时间戳。

JavaScript use Date.parse when creating a Date object from a timestamp. 从时间戳创建Date对象时,JavaScript使用Date.parse In desktop versions of many browsers, they accept MySQL timestamps to create Date objects. 在许多浏览器的桌面版本中,它们都接受MySQL时间戳来创建Date对象。 But in iOS and IE, this doesn't work. 但是在iOS和IE中,这不起作用。 For date to be created using a string, the timestamp should be in the ISO format. 若要使用字符串创建日期,则时间戳记应为ISO格式。 Therefore, it do not accept MySQL timestamps. 因此,它不接受MySQL时间戳。

To solve this, I referred this question where the timestamp was split and then used to create the date object. 为了解决这个问题,我提到了这个问题 ,即在哪里拆分了时间戳,然后将其用于创建日期对象。

angular.module('HIS')
    .filter('dateToISO', function () {
        return function (input) {
            var t = input.split(/[- :]/);
            var date = new Date(Date.UTC(t[0], t[1]-1, t[2], t[3], t[4], t[5]));
            return date;
        };
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM