function getDisplayDate(year, month, day) {
today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
today.setMilliseconds(0);
compDate = new Date(year,month-1,day); // month - 1 because January == 0
diff = today.getTime() - compDate.getTime(); // get the difference between today(at 00:00:00) and the date
if (compDate.getTime() == today.getTime()) {
return "Today";
} else if (diff <= (24 * 60 * 60 *1000)) {
return "Yesterday";
} else {
//return compDate.toDateString(); // or format it what ever way you want
year = compDate.getFullYear();
month = compDate.getMonth();
months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
day = compDate.getDate();
d = compDate.getDay();
days = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
var formattedDate = days[d] + " " + day + " " + months[month] + " " + year;
return formattedDate;
}
}
这是@xblitz 用我的格式回答以一种很好的方式显示日期。