Check if date is today, was yesterday or was in the last 7 days

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.

26 thoughts on “Check if date is today, was yesterday or was in the last 7 days”

  1. Okay, we can do it in these steps:

    1. Get the two dates’ Date() objects.
    2. Set the time using .setTime() to a particular time for both.
    3. Using .getTime(), calculate the milliseconds.
    4. Make the calculation of time for both the dates.
    5. Check the following cases:
      1. If the difference is 86400000, it is yesterday.
      2. If the difference is a multiple of 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";
    
    Reply
  2. You can use the date difference between two dates it will return difference in days if there is minus value then like

    var date1 = new Date("12/15/2010");
    var date2 = new Date("12/14/2010");
    var timeDiff = date2.getTime() - date1.getTime();
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
    if(diffDays == -1){ alert("Yesterday")}
    
    Reply
  3. You can use this function and pass your date as a parameter and it will return day of week and after 7 days it will return given date.

    function findDay(date) {
        let dateToCheck = new Date(date);
        
        const today = new Date();
        const yesterday = new Date(today);
        const oneDayBeforeYesterday = new Date(yesterday);
        const twoDayBeforeYesterday = new Date(yesterday);
        const threeDayBeforeYesterday = new Date(yesterday);
        const fourDayBeforeYesterday = new Date(yesterday);
        const fiveDayBeforeYesterday = new Date(yesterday);
    
        yesterday.setDate(yesterday.getDate() - 1);
        oneDayBeforeYesterday.setDate(yesterday.getDate() - 1);
        twoDayBeforeYesterday.setDate(yesterday.getDate() - 2);
        threeDayBeforeYesterday.setDate(yesterday.getDate() - 3);
        fourDayBeforeYesterday.setDate(yesterday.getDate() - 4);
        fiveDayBeforeYesterday.setDate(yesterday.getDate() - 5);
    
        let days = [
            "Sunday",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
        ];
    
        if (dateToCheck.toDateString() === today.toDateString()) {
            return "Today";
        } else if (dateToCheck.toDateString() === yesterday.toDateString()) {
            return "Yesterday";
        } else if (dateToCheck.toDateString() === oneDayBeforeYesterday.toDateString()) {
            return days[myDate.getDay()];
        } else if (dateToCheck.toDateString() === twoDayBeforeYesterday.toDateString()) {
            return days[myDate.getDay()];
        } else if (dateToCheck.toDateString() === threeDayBeforeYesterday.toDateString()) {
            return days[myDate.getDay()];
        } else if (dateToCheck.toDateString() === fourDayBeforeYesterday.toDateString()) {
            return days[myDate.getDay()];
        } else if (dateToCheck.toDateString() === fiveDayBeforeYesterday.toDateString()) {
            return days[dateToCheck.getDay()];
        } else {
            return dateToCheck;
        }
    }
    
    console.log(findDay("07-10-2020"));
    Reply
  4. 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 = dt = new Date(someDateTimeStamp),
            date = dt.getDate(),
            month = months[dt.getMonth()],
            timeDiff = someDateTimeStamp - Date.now(),
            diffDays = new Date().getDate() - date,
            diffYears = new Date().getFullYear() - dt.getFullYear();
    
        if(diffYears === 0 && diffDays === 0){
          return "Today";
        }else if(diffYears === 0 && diffDays === 1) {
          return "Yesterday";
        }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() - (1000 * 3600 * 24 * 1)) //"Yesterday"
    formatDate(Date.now() - (1000 * 3600 * 24 * 2)) // it will return the name of the week if it is beyond two days
    
    Reply
    1. Why don’t you try something like this
    2. You can do this also for 7 days ago:

      export function isToday(date, now) {
        const yearDate = date.getYear();
        const monthDate = date.getMonth();
        const dayDate = date.getDate();
        const yearNow = now.getYear();
        const monthNow = now.getMonth();
        const dayNow = now.getDate();
        if (yearDate === yearNow && monthDate === monthNow && dayDate === 
          dayNow) {
          return true
        }
        return false
      }
      
    Reply
  5. This works for me:

      private _isSameDay(date1: Date, date2: Date): boolean {
        return (
          date1.getUTCFullYear() === date2.getUTCFullYear() &&
          date1.getMonth() === date2.getMonth() &&
          date1.getDate() === date2.getDate()
        );
      }
    
      private _getSeparatorBody(date: Date): string {
        if (this._isSameDay(date, new Date())) {
          return 'Today';
        }
        const yesterday = new Date();
        yesterday.setDate(yesterday.getDate() - 1);
        if (this._isSameDay(date, yesterday)) {
          return 'Yesterday';
        }
        return date.toISOString();
      }
    
    Reply

Leave a Comment