0

I have this String :

var str = "Thu, 10 Apr 2014 09:19:08 +0000";

I would like to get this format : "10 Apr 2014"

How can i do that?

1
  • 1
    Are you trying to format time from a timestamp or just a bare string of characters? Commented Apr 10, 2014 at 9:45

4 Answers 4

1
    var str = "Thu, 10 Apr 2014 09:19:08 +0000";
    var d = new Date(str);
   var month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var b=  d.getDate()+' '+month[d.getMonth()]+' '+d.getFullYear();
    alert(b);

Check the result in JSFiddle

Sign up to request clarification or add additional context in comments.

Comments

1

You can split the string on spaces and take the second to the fourth items and join:

var d = str.split(' ').slice(1, 4).join(' ');

Demo: http://jsfiddle.net/Guffa/7FuD6/

Comments

0

you can use the substring() method like this,

 var str = "Thu, 10 Apr 2014 09:19:08 +0000";
 var res = str.substring(5,15);

1 Comment

Yes, but there is no other way to do this using Date type ?
0
var str = "Thu, 10 Apr 2014 09:19:08 +0000",
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    d = new Date(str);

d.getDate() + " " + months[d.getMonth()] + " " + d.getFullYear();  //"10 Apr 2014" 

The date string you have can be passed into the Date constructor to get a date object, d. The date object has various methods to that gives day, year, month, time etc. Since months are returned as an integer and we need the name, we use an array called months.

1 Comment

The Thom: Added some explanation.

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.