我有一个MM / DD / YYYY格式的字符串。
有人可以告诉我如何将其转换为:2011年1月1日?
答案 0 :(得分:4)
如果您还没有,请查看date.js -
这对于任何与日期相关的功能都很棒,并且在他们的网站上有大量的例子来做你能想象到的一切。
更新:好的,有些人怀疑我疯狂的编码技巧:)
<script src="http://www.datejs.com/build/date.js" type="text/javascript"></script>
<script language="Javascript">
var d = Date.parse('03/08/1980');
window.alert(d.toString('MMMM d, yyyy'));
</script>
答案 1 :(得分:2)
var str = "01/01/2011",
date = new Date(str),
months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"],
converted = months[date.getMonth()] + " " +
date.getDate() + ", " + date.getFullYear();
答案 2 :(得分:1)
var str = "1/1/2011";
var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
var parts = str.split("/");
var formatted = monthNames[parseInt(parts[0], 10)-1] + " " + parts[1] + ", " + parts[2];
答案 3 :(得分:1)
我希望这会有所帮助
var dat = new Date("01/01/2011");
var monthNames = [ "January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December" ];
var stringDate = monthNames[dat.getMonth()] + ", " + dat.getDate() + ", " + dat.getFullYear();
答案 4 :(得分:1)
我写了一个类似于DateJS的日期库,只是它更小,更快,并且不会修改Date.prototype
。它处理解析,操作和格式化,包括timeago。
我看到你只需要英语,但在underscore.date中支持i18n。
https://github.com/timrwood/underscore.date
使用underscore.date,这就是解决问题的方法。
_date('01/01/2011', 'MM/DD/YYYY').format('MMMM D, YYYY');