在JavaScript中将日期转换为字符串

时间:2016-02-29 11:05:31

标签: javascript jquery

我有一个约会" 2016/02/22"我想让最终用户更具可读性。我尝试过使用toString()方法,但没有运气。

EG。 " 2016年2月2日星期一"

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:0)

var dateObj = new Date(Date.parse("2016/02/22"));
var week = [ "Sunday", "Monday", "Tueday", "Wednesday", "Thursday", "Friday", "Saturday" ];
var month = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
var displayWeek = week[dateObj.getDay()];
var displayDate = dateObj.getDate();
switch(displayDate)
{
   case 1: displayDate = "1st"; break;
   case 2: displayDate = "2nd"; break;
   case 3: displayDate = "3rd"; break;
   case 21: displayDate = "21st"; break;
   case 22: displayDate = "22nd"; break;
   case 23: displayDate = "23rd"; break;
   case 31: displayDate = "31st"; break;
   default: displayDate += "th"; break;
}
var displayMonth = month [dateObj.getMonth()];
var displayYear = dateObj.getFullYear();
console.log( displayWeek  + " " + displayDate  + " " + displayMonth + " " + displayYear  );

答案 1 :(得分:0)

var d = new Date("2016/02/22");
console.log(d.toDateString());

如果你提到你想要显示日期的格式会更好。