格式1: 2014年5月12日
格式2: 2014年5月12日星期一
这是我的尝试
function formatDate(date) {
var weekday = new Array(7);
weekday[0] = "Sun";
weekday[1] = "Mon";
weekday[2] = "Tue";
weekday[3] = "Wed";
weekday[4] = "Thu";
weekday[5] = "Fri";
weekday[6] = "Sat";
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var month = (date.getMonth()) + 1;
month = "0" + month;
month = month.slice(-2);
//var out = weekday[date.getDay()] + " " + monthNames[date.getMonth()] + " " + date.getDate() + " " + date.getFullYear();
var out = +date.getDate() + " " + weekday[date.getDay()];
return out;
}
答案 0 :(得分:0)
请查看MomentJS,它提供了多种功能来轻松处理这类问题。
答案 1 :(得分:0)
JavaScript new Date()
函数在接受多种格式的日期时非常聪明。
我喜欢 10 ways to format time and date using JavaScript 和 Working with Dates
基本上,你有三种方法,你必须自己组合字符串:
getDate() // Returns the date
getMonth() // Returns the month
getFullYear() // Returns the year
示例强>
<script type="text/javascript">
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
document.write(curr_date + "-" + curr_month + "-" + curr_year);
</script>
via - Haim