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!

1

2 Answers 2

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];
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.