user2811083
user2811083

Reputation: 83

How to convert date of RSS feed to Dutch date with javascript?

This is the date i got out of the RSS feed: "2014-02-28T20:00:00+0100"

Now i want it like this: 28 Februari 2014 - 20:00

Can somebody help me?

Upvotes: 1

Views: 3158

Answers (3)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241748

If you can limit yourself to the latest browsers that support the ECMAScript Internationalization API, then you can do the following:

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);

The result is "28 februari 2014 11:00". If you really need the hyphen in there:

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);

Also, watch the offset of the input string. Some browsers (like Internet Explorer) won't take accept the ISO8601 format with +0100 as the offset, but require +01:00 fully extended.

And as anurag_29 pointed out, if you need to do this cross-browser, and in older browsers, your best bet is moment.js.

Upvotes: 2

Renato Galvones
Renato Galvones

Reputation: 555

RegExp can solve this.

You'll only need an array to do the comparison with the months and change it to the according month in Dutch.

JsFiddle is here: http://jsfiddle.net/JdLBs/

Code here:

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);

Upvotes: 1

anurag_29
anurag_29

Reputation: 932

have a look at moment.js. It returns the date in any format you want : http://momentjs.com/

Upvotes: 3

Related Questions