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)
I see that something like that is possible with php. Here - PHP: date "Yesterday", "Today"
Is this possible using javascript?
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)
I see that something like that is possible with php. Here - PHP: date "Yesterday", "Today"
Is this possible using javascript?
Share Improve this question edited May 23, 2017 at 11:43 CommunityBot 11 silver badge asked Jan 15, 2013 at 13:44 MichaelMichael 4,40011 gold badges56 silver badges91 bronze badges3 Answers
Reset to default 3I 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);
pDate = new Date(year,month-1,day); // month - 1 because January == 0
diff = today.getTime() - pDate.getTime(); // get the difference between today(at 00:00:00) and the date
if (pDate.getTime() == today.getTime()) {
return "Today";
} else if (diff <= (24 * 60 * 60 *1000)) {
return "Yesterday";
} else {
return pDate.toDateString(); // or format it what ever way you want
}
}
than you should be able to get the date like this:
getDisplayDate(2013,01,14);
This is a pilation of these two answers (and should give you a great start):
- What's the best way to calculate date difference in Javascript
- Javascript show milliseconds as days:hours:mins without seconds
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
function getDisplayDate(year, month, day) {
today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
today.setMilliseconds(0);
pDate = new Date(year,month-1,day); // month - 1 because January == 0
diff = today.getTime() - pDate.getTime(); // get the difference between today(at 00:00:00) and the date
if (pDate.getTime() == today.getTime()) {
return "Today";
} else if (diff <= (24 * 60 * 60 *1000)) {
return "Yesterday";
} else {
//return pDate.toDateString(); // or format it what ever way you want
year = pDate.getFullYear();
month = pDate.getMonth();
months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
day = pDate.getDate();
d = pDate.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.