8

I am creating an simple email client and I want the inbox to display the date that the email was received in the format:

Today at 13:17

Yesterday at 20:38

13 January at 17:15

21 December 2012 @ 18:12

I am retrieving the data from a database, outputting it to xml (so everything can be done through AJAX) and printing the results to a <ul><li> format.

The Date and Time are stored separately in the format:

Date(y-m-d)

Time(H:i:s)

what i have so far

I see that something like that is possible with php. Here - PHP: date "Yesterday", "Today"

Is this possible using javascript?

Community
  • 1
  • 1
Michael
  • 4,282
  • 9
  • 55
  • 89

3 Answers3

3

I would go about with something like this

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
    }
}

than you should be able to get the date like this:

getDisplayDate(2013,01,14);
xblitz
  • 663
  • 4
  • 14
1

This is a compilation of these two answers (and should give you a great start):

I suggest reading both questions and the responses to get a better idea of what is going on.


function DateDiff(date1, date2) {
    return dhm(date1.getTime() - date2.getTime());
}

function dhm(t){
    var cd = 24 * 60 * 60 * 1000,
        ch = 60 * 60 * 1000,
        d = Math.floor(t / cd),
        h = '0' + Math.floor( (t - d * cd) / ch),
        m = '0' + Math.round( (t - d * cd - h * ch) / 60000);
    return [d, h.substr(-2), m.substr(-2)].join(':');
}

var yesterdaysDate = new Date("01/14/2013");
var todaysDate = new Date("01/15/2013");

// You'll want to perform your logic on this result
var diff = DateDiff(yesterdaysDate, todaysDate); // Result: -1.00
Community
  • 1
  • 1
James Hill
  • 60,353
  • 20
  • 145
  • 161
1
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;
    }
}

This is @xblitz answer with my formatting to show the date in a nice way.

Michael
  • 4,282
  • 9
  • 55
  • 89