最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS Date filter firefoxsafari issues - Stack Overflow

programmeradmin2浏览0评论

I'm having issues getting a date to display properly in Firefox and Safari, using a custom filter which supports day suffixs. I get the UTC date in the format:

yyyy-mm-dd hh-mm-ss

I then have a custom DateFilter, which replaces oo with a suffix, i.e. 2nd:

var suffixes = ["th", "st", "nd", "rd"];

return function(input, format) {
    input = new Date(input).getTime();
    var dtfilter = $filter('date')(input, format);
    var day = parseInt($filter('date')(input, 'dd'));
    var relevantDigits = (day < 30) ? day % 20 : day % 30;
    var suffix = (relevantDigits <= 3) ? suffixes[relevantDigits] : suffixes[0];

    return dtfilter.replace('oo', suffix);
};

This works on Chrome, I pass the following into my template, and get the expected date:

{{ date.date_utc | DateFilter:'EEEE MMMM doo yyyy' | uppercase }} = SATURDAY NOVEMBER 1ST 2014

On Firefox/Safari this is returned as:

UNDEFINED UNDEFINED NANTH 0NAN

Research from here and here suggest I need to pass the ISO time, or a timestamp into my Date object.

I seem to already be doing this via .getTime(). I've also tried .toISOString(), but this doesn't even return anything in Firefox/Safari!

Any ideas?

I'm having issues getting a date to display properly in Firefox and Safari, using a custom filter which supports day suffixs. I get the UTC date in the format:

yyyy-mm-dd hh-mm-ss

I then have a custom DateFilter, which replaces oo with a suffix, i.e. 2nd:

var suffixes = ["th", "st", "nd", "rd"];

return function(input, format) {
    input = new Date(input).getTime();
    var dtfilter = $filter('date')(input, format);
    var day = parseInt($filter('date')(input, 'dd'));
    var relevantDigits = (day < 30) ? day % 20 : day % 30;
    var suffix = (relevantDigits <= 3) ? suffixes[relevantDigits] : suffixes[0];

    return dtfilter.replace('oo', suffix);
};

This works on Chrome, I pass the following into my template, and get the expected date:

{{ date.date_utc | DateFilter:'EEEE MMMM doo yyyy' | uppercase }} = SATURDAY NOVEMBER 1ST 2014

On Firefox/Safari this is returned as:

UNDEFINED UNDEFINED NANTH 0NAN

Research from here and here suggest I need to pass the ISO time, or a timestamp into my Date object.

I seem to already be doing this via .getTime(). I've also tried .toISOString(), but this doesn't even return anything in Firefox/Safari!

Any ideas?

Share Improve this question edited May 23, 2017 at 12:25 CommunityBot 11 silver badge asked Nov 8, 2014 at 12:38 AliasAlias 3,0918 gold badges41 silver badges62 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

It seems Firefox/Safari doesn't parse .toISOString() into the 'correct' format...

input = input.replace(/(.+) (.+)/, "$1T$2Z");
input = new Date(input).getTime();

This now parses it into a correct ISO format, and then is successfully parsed.

If you are passing the date in this format: 2021-01-29 12:10:58 safari will throw an error.

It must be in this format: 2021-01-29T12:10:58

发布评论

评论列表(0)

  1. 暂无评论