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?
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?
- 1 you would need to use a little bit of javascript here. see this question - stackoverflow.com/questions/1056728/… – Arvind Sridharan Commented Dec 7, 2013 at 6:24
- What are you using to pick the date? – Kawinesh S K Commented Dec 7, 2013 at 6:24
5 Answers
Reset to default 4Absolutely with the wonderful MomentJS.
dob = moment(dob).format('DD MMMM YYYY');
If your date is an instance of Date
then 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).
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"));
Use moment.js, and it will be a snap.
moment(dob).format('DD MMMM YYYY')
You can use Date.js library which extents Date object.