Help with Date Conversion in JavaScript
I开发者_如何学JAVA have a string in the format MM/DD/YYYY.
Can someone tell me how to convert it to: January 1, 2011?
If you haven't already, check out date.js -
http://www.datejs.com/
It's great for any date related functions, and has tons of examples on their site to do everything you can imagine.
Update: Ok some people doubt my mad coding skillz :)
<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>
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();
See it in action.
I hope this helps
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();
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];
I wrote a date library similar to DateJS, only it's smaller, faster, and doesn't modify the Date.prototype
. It handles parsing, manipulating, and formatting, including timeago.
I see that you only need english, but there is support for i18n in underscore.date.
https://github.com/timrwood/underscore.date
With underscore.date, this is how you would solve your problem.
_date('01/01/2011', 'MM/DD/YYYY').format('MMMM D, YYYY');
精彩评论