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
  • 43,214
  • 14
  • 119
  • 136
user3004356
  • 870
  • 4
  • 16
  • 49

5 Answers5

4

Absolutely with the wonderful MomentJS.

dob = moment(dob).format('DD MMMM YYYY');
CWSpear
  • 3,230
  • 1
  • 28
  • 34
3

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,890
  • 3
  • 22
  • 27
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
  • 169,008
  • 28
  • 173
  • 236
Rhyono
  • 2,420
  • 1
  • 25
  • 41
1

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

moment(dob).format('DD MMMM YYYY')
Vidya
  • 29,932
  • 7
  • 42
  • 70
  • 3
    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,785
  • 2
  • 24
  • 37