1

I am getting a data string in jQuery in the following format: YYYY-MM-DD.
Now I want to show it like this: 24 Aug 2014.
It can be done easily in php by using date function but do not know in jQuery.

$("#txt_birth_date").html(' ' + ((result.birth_date) === null) ? '' : result.birth_date));
steinerkelvin
  • 475
  • 2
  • 12
Aksh
  • 654
  • 6
  • 13

3 Answers3

4

Moment.js has great documentation.

http://momentjs.com

Jeremy Danyow
  • 26,470
  • 12
  • 87
  • 133
  • it is really a good thing.i also want to knoe that is there any other way to do this without using momentjs or Datejs? – Aksh Apr 04 '14 at 11:06
  • I use moment.js and would recommend it to anyone dealing with dates in javascript- whether it be formatting dates, parsing dates or date computation. Don't re-invent the wheel. – Jeremy Danyow Apr 04 '14 at 12:12
2

Date formatting or date calculation in javascript is not as straight forward as in php. I'm using the Datejs library:

Vickel
  • 7,879
  • 6
  • 35
  • 56
0

I recommend you to use a library like this: https://github.com/phstc/jquery-dateFormat.

But if you want to format a string from 2014-08-24 into 24 Aug 2014, you can wirte something like this:

function myFormatDate(d){
    var mothes   = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun','Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var d_y = d.split('-')[0];
    var d_m = d.split('-')[1];
    var d_d = d.split('-')[2];

    var mon = mothes[parseInt(d_m,10)-1];
    var dd = parseInt(d_d,10);

    if(d_y == "0000"){ // EDITED 
        return  dd + " " + mon;
    }else{
        return  dd + " " + mon + " " + d_y;
    }
}
var d = "2014-08-24";
alert(myFormatDate(d));

Hope this helps.

naota
  • 4,695
  • 1
  • 18
  • 21