wawanopoulos
wawanopoulos

Reputation: 9804

Convert Javascript String to specific format

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?

Upvotes: 0

Views: 73

Answers (4)

user235273
user235273

Reputation:

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.

Upvotes: 0

Venkadeshwaran
Venkadeshwaran

Reputation: 26

    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

Upvotes: 1

Guffa
Guffa

Reputation: 700720

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/

Upvotes: 1

Balayesu Chilakalapudi
Balayesu Chilakalapudi

Reputation: 1406

you can use the substring() method like this,

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

Upvotes: 0

Related Questions