最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert date to words in HTML - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question edited Dec 7, 2013 at 6:33 m59 43.8k14 gold badges121 silver badges139 bronze badges asked Dec 7, 2013 at 6:17 user3004356user3004356 8804 gold badges17 silver badges49 bronze badges 2
  • 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
Add a comment  | 

5 Answers 5

Reset to default 4

Absolutely with the wonderful MomentJS.

dob = moment(dob).format('DD MMMM YYYY');

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).

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.

发布评论

评论列表(0)

  1. 暂无评论