5

I have a date displayed in HTML. The date is currently displayed as 2012-03-12. So now, I want to display this date as words i.e it should be displayed as 12 March 2012. Below is the HTML code I used.

<tr>
  <th>Date of Birth: </th>
  <td>{{dob}}</td>
</tr>  

Here, dob contains the value that has to be converted to words. How can I do this?

m59
  • 42,346
  • 14
  • 112
  • 132
user3004356
  • 860
  • 3
  • 16
  • 47

5 Answers5

3

Absolutely with the wonderful MomentJS.

dob = moment(dob).format('DD MMMM YYYY');
CWSpear
  • 3,170
  • 1
  • 26
  • 34
2

If you don't want to use any library and want to take a date like your initial one and change it, it can be done like this:

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

function convertDate(date_str) {
  temp_date = date_str.split("-");
  return temp_date[2] + " " + months[Number(temp_date[1]) - 1] + " " + temp_date[0];
}
console.log(convertDate("2012-03-12"));
mplungjan
  • 155,085
  • 27
  • 166
  • 222
Rhyono
  • 2,370
  • 24
  • 39
2

If your date is an instance of Datethen you can try something like this

var dob = new Date('3/12/2012');
var dobArr = dob.toDateString().split(' ');
var dobFormat = dobArr[2] + ' ' + dobArr[1] + ' ' + dobArr[3];

This would make dobFormat 12 Mar 2012 (if you want it to say March couple this with what Rhyono has suggested).

tewathia
  • 6,380
  • 3
  • 21
  • 27
1

Use moment.js, and it will be a snap.

moment(dob).format('DD MMMM YYYY')
Vidya
  • 29,172
  • 6
  • 39
  • 65
  • 1
    so much dependency on libs :| – Pal Singh Dec 07 '13 at 06:33
  • Neither @CWSpear or I meant that you print this out literally. Rather, you pass the string in `dob` to `moment` as shown and store the output in a template variable to display in the `td` cell. – Vidya Dec 07 '13 at 15:00
0

You can use Date.js library which extents Date object.

Prateek
  • 6,629
  • 2
  • 23
  • 37