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.
To check if a date is yesterday:Subtract 1 day from the current date to get yesterday's date.
To check if a date is today's date:If the year, month and day values are equal, then the date is today's date.
We use getDate() function to fetch the current date from the date object and subtract one day from it using the setDate() function which sets yesterday's date onto the date object.
There are two ways to check if two dates are equal in Java : Date's equals() method - return true if two dates are equal. Date's compareTo() method - return zero if two dates are equal.
If you want to print today, yesterday, display the day of the week, display date without year if current year and display date with the year if the previous year, below code will help you.
var fulldays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
function formatDate(someDateTimeStamp) {
var dt = new Date(someDateTimeStamp),
date = dt.getDate(),
month = months[dt.getMonth()],
timeDiff = someDateTimeStamp - Date.now(),
diffDays = new Date().getDate() - date,
diffMonths = new Date().getMonth() - dt.getMonth(),
diffYears = new Date().getFullYear() - dt.getFullYear();
if(diffYears === 0 && diffDays === 0 && diffMonths === 0){
return "Today";
}else if(diffYears === 0 && diffDays === 1) {
return "Yesterday";
}else if(diffYears === 0 && diffDays === -1) {
return "Tomorrow";
}else if(diffYears === 0 && (diffDays < -1 && diffDays > -7)) {
return fulldays[dt.getDay()];
}else if(diffYears >= 1){
return month + " " + date + ", " + new Date(someDateTimeStamp).getFullYear();
}else {
return month + " " + date;
}
}
formatDate(Date.now()) //"Today"
formatDate(Date.now() - 86400000) // "Yesterday"
formatDate(Date.now() - 172800000) // it will return the name of the week if it is beyond two days
Okay, we can do it in these steps:
Date()
objects..setTime()
to a particular time for both..getTime()
, calculate the milliseconds.86400000
, it is yesterday.86400000
, it is the number of days.JavaScript Code
var a = new Date(2015, 8 - 1, 25); // Today
var b = new Date(2015, 8 - 1, 24); // Yesterday
var c = new Date(); // Now
c.setHours(0);
c.setMinutes(0);
c.setSeconds(0, 0);
if (a.getTime() == c.getTime())
return "Today";
else if (b.getTime() == c.getTime())
return "Yesterday";
else if ((new Date(2015, 8 - 1, 25 - 7)).getTime() < c.getTime())
return "Less than a week";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With