我想要实现的是当我在这个函数中输入一个日期时,它应该给我这个:
如果日期是:
今天 - 输出为Today
明天 - 输出为Tomorrow
昨天 - 输出为Yesterday
Else - 输出为“月 - 日”格式
但我无法让它工作。有什么帮助吗?JsFiddle在这里。
//My Date variable
var mydate = "22-Nov-2012"
alert(MDFormat(mydate));
/*
If the date is:
Today - show as "Today";
Tomorrow - show as "Tomorrow"
Yesterday - show as "Yesterday"
Else - show in "Month - Day format"
*/
function MDFormat(MMDD) {
MMDD= new Date(MMDD);
var months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var day = MMDD.getDate();
var month = months[MMDD.getMonth()];
var currentDay = new Date().getDay();
var today;
if (currentDay == MMDD.getDay()) {
today = MMDD.getDay();
}
var tmr = currentDay + 1;
var yest = currentDay - 1;
var strDate;
switch (MMDD) {
case today:
strDate = "Today";
break;
case tmr:
strDate = "Tomorrow";
break;
case yest:
strDate = "Yesterday";
break;
default:
strDate = month + "-" + day;
break;
}
return strDate;
}
注意:我更喜欢无插件的方式。