Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Date Format [Today/Tomorrow/Yesterday/Month-Day] - can't get this to work

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.

like image 547
abdhoms Avatar asked Dec 27 '22 14:12

abdhoms


1 Answers

getDate only works on valid predefined date formats. The one you are using is not a valid predefined format. If you try this:

var mydate = "Nov 22, 2012";
alert(MDFormat(mydate));

it works.

Also took the liberty of fixing up a few problems with your logic:

//My Date variable
var mydate = "Nov 22, 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 strDate = "";

    var today = new Date();
    today.setHours(0, 0, 0, 0);

    var yesterday = new Date();
    yesterday.setHours(0, 0, 0, 0);
    yesterday.setDate(yesterday.getDate() - 1);

    var tomorrow = new Date();
    tomorrow.setHours(0, 0, 0, 0);
    tomorrow.setDate(tomorrow.getDate() + 1);

    console.log(MMDD.getTime(),today.getTime(),MMDD.getTime()==today.getTime());

    if (today.getTime() == MMDD.getTime()) {
        strDate = "Today";
    } else if (yesterday.getTime() == MMDD.getTime()) {
        strDate = "Yesterday";
    } else if (tomorrow.getTime() == MMDD.getTime()) {
        strDate = "Tomorrow";
    } else {
        strDate = months[MMDD.getMonth()] + "-" + MMDD.getDate();
    }

    return strDate;
}

Demonstration: http://jsfiddle.net/xqnc8/4/

like image 115
Asad Saeeduddin Avatar answered May 12 '23 01:05

Asad Saeeduddin