【问题标题】:How to convert date of RSS feed to Dutch date with javascript?如何使用 javascript 将 RSS 提要的日期转换为荷兰日期?
【发布时间】:2014-02-28 13:55:03
【问题描述】:

这是我从 RSS 提要中得到的日期:“2014-02-28T20:00:00+0100”

现在我想要这样:2014 年 2 月 28 日 - 20:00

有人可以帮帮我吗?

【问题讨论】:

  • 请记住,荷兰语中的月份名称不大写。

标签: javascript jquery date time


【解决方案1】:

看看 moment.js。它以您想要的任何格式返回日期:http://momentjs.com/

【讨论】:

  • 不想使用它,因为它对我的应用程序来说太大了。有没有其他方法可以做到这一点?
  • 好的!我想你可以使用 javascript 的 Date 函数,如:var date = new Date("2014-02-28T20:00:00+0100")。我想这应该可行。
【解决方案2】:

如果您可以限制自己使用支持ECMAScript Internationalization API 的最新浏览器,那么您可以执行以下操作:

var dt = new Date("2014-02-28T20:00:00+01:00");

var options = { year:'numeric',
                month:'long',
                day:'numeric',
                hour:'numeric',
                minute:'numeric'};

var s = dt.toLocaleString('nl', options);

结果是"28 februari 2014 11:00"。如果您确实需要其中的连字符:

var dt = new Date("2014-02-28T20:00:00+01:00");

var dateOptions = { year:'numeric', month:'long', day:'numeric' };
var timeOptions = { hour:'numeric', minute:'numeric'};

var s = dt.toLocaleDateString('nl', dateOptions) + ' - ' + 
        dt.toLocaleTimeString('nl', timeOptions);

另外,观察输入字符串的偏移量。某些浏览器(如 Internet Explorer)不会接受以+0100 作为偏移量的 ISO8601 格式,但需要 +01:00 完全扩展。

正如 anurag_29 指出的那样,如果您需要跨浏览器,并且在旧版浏览器中,最好的选择是 moment.js

【讨论】:

    【解决方案3】:

    RegExp 可以解决这个问题。

    您只需要一个数组来与月份进行比较并将其更改为荷兰语中的相应月份。

    JsFiddle 在这里:http://jsfiddle.net/JdLBs/

    代码在这里:

    var date = "2014-02-28T20:00:00+0100";
    var search = /(.*)T(.*)\+/i;
    var result = date.match(search);
    
    var tempYear = result[1].split('-');
    var tempTime = result[2].split(':');
    
    var dutchDate = tempYear[2] + ' ' + tempYear[1] + ' ' + tempYear[0] + ' - ' + tempTime[0] + ':' + tempTime[1];
    
    console.log(dutchDate);
    

    【讨论】:

      猜你喜欢
      • 2015-07-27
      • 1970-01-01
      • 2017-04-15
      • 2020-03-19
      • 2019-06-14
      • 2010-12-18
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多