What I am trying to achieve is when I input a date into this function, it should give me this:
If the date is:
Today – output as Today
Tomorrow – output as Tomorrow
Yesterday – output as Yesterday
Else – output as “Month – Day” format
But I can’t get this to work. Any help? JsFiddle here.
//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;
}
Note: I prefer a plugin-less way.
getDateonly works on valid predefined date formats. The one you are using is not a valid predefined format. If you try this:it works.
Also took the liberty of fixing up a few problems with your logic:
Demonstration: http://jsfiddle.net/xqnc8/4/