I know of this thread: Elegantly check if a given date is yesterday
But I'm just specifically looking for a JavaScript solution. If possible a short one. I couldn't really figure out a 100% reliable way..
This is how I have done it so far:
function FormatDate(someDtUTC) {
var someDt = new Date(someDtUTC.getTime() + someDtUTC.getTimezoneOffset() * 60 * 1000);
var dtNow = new Date();
if (dtNow.getUTCFullYear() == someDt.getUTCFullYear() && dtNow.getUTCMonth() == someDt.getUTCMonth()) {
if (dtNow.getUTCDate() == someDt.getUTCDate())
var dateString = "Today, " + Ext.Date.format(someDt, 'G:i'); // Today, 15:32
else if (dtNow.getUTCDate() - 1 == someDt.getUTCDate())
var dateString = "Yesterday, " + Ext.Date.format(someDt, 'G:i'); //Yesterday, 13:26
else if (dtNow.getUTCDate() - someDt.getUTCDate() < 7)
var dateString = Ext.Date.format(someDt, 'l, G:i'); //Sunday, 14:03
} else
var dateString = Ext.Date.format(someDt, 'j.n.y\, G:i'); //7.8.15, 8:25
return dateString;
}
Don't worry about the Ext.Date.format()
function, it's not part of the question.
The problem with that code is, that it can't handle situations like:
Today: 01.08.15
Yesterday: 31.07.15
Any idea how I could tell the function to handle that as well?
I'm not looking for a solution with exterenal libraries (that includes ExtJS). I'd like to solve this with raw JavaScript.