我有以下日期格式:2010-04-15 23:59:59
我如何将其转换为:15th Apr 2010
使用javascript
答案 0 :(得分:3)
结帐Datejs。您可能想要解析时间戳,然后在其上调用toString('MMS MMM yyyy')
(或其他类似的东西)。以下是toString的更多信息。
如果这就是你所追求的,那么可能会有更轻量级的解决方案。
答案 1 :(得分:1)
像dylanfm建议的那样,我会查看Datejs,但如果您只想转换您描述的方式,这可能对您有用:
var m = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var p = ["th","st","nd","rd"];
// create date object - have to replace dashes with slashes
var d = new Date("2010-04-15 23:59:59".replace(/-/g,"/"));
// index in array p based on date % 10
var pn = d.getDate() % 10;
// pick "th" for days 11-13
if (d.getDate() > 10 && d.getDate() < 14 ) pn = 0;
// pick "th" for days 4-9, 14-19, 24-29
if (pn >= p.length) pn = 0;
// date in format "15th Apr 2010"
var formatted = d.getDate() + p[pn] + " " + m[d.getMonth()] + " " + d.getFullYear();
请注意,这远不如使用Datejs这样的库那么灵活。