1

I've been struggling with javascript more than an hour and came up with a solution - to ask you for help!

A RSS Feed generates the date of every post in this format 2011-05-18T17:32:43Z. How can I make it look like that 17:32 18.05.2011?

Thank you in advance!

FakeHeal
  • 53
  • 4
  • 1
    possible duplicate of [Formatting a date in javascript](http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript) – Jeremy May 19 '11 at 18:47

2 Answers2

1

Assuming you've parsed the RSS date into a JS Date object (which can be tricky, since many Date.parse implementations don't accept ISO-8601 dates like that)...

//var d=new Date(/*...*/)
// 17:32 18.05.2011
pad(d.getHours())+':'+d.getMinutes()+' '+
  pad(d.getDate())+'.'+pad(d.getMonth()+1)+d.getFullYear();

(getMonth returns 0-11 based month)

... you'd also want some kind of zero buffering for the month (in your example) and possibly day, hour (depending)....

function pad(val,len) {
  var s=val.toString();
  while (s.length<len) {s='0'+s;}
  return s;
}

Optionally from string->string you could use:

function reformat(str) {
  var isodt=string.match(/^\s*(\-?\d{4}|[\+\-]\d{5,})(\-)?(\d\d)\2(\d\d)T(\d\d)(:)?(\d\d)?(?:\6(\d\d))?([\.,]\d+)?(Z|[\+\-](?:\d\d):?(?:\d\d)?)\s*$/i);
  if (isodt===null) {return '';} // FAILED
  return isodt[5]+':'+isodt[7]+' '+isodt[4]+'.'+isodt[3]+'.'+isodt[1];
}
Rudu
  • 15,682
  • 4
  • 47
  • 63
0

You can create a new Date, get the fragments out of it and concatenate everything:

function parse(date) {
    var d = new Date(date)
    return d.getHours() + ":" + d.getMinutes() 
          + " " + d.getDate() + "." + (d.getMonth()+1)
          + "." + d.getFullYear();
}
pimvdb
  • 151,816
  • 78
  • 307
  • 352