Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 203
Forivin Avatar asked Aug 25 '15 09:08

Forivin


People also ask

How do you know if a date was yesterday?

To check if a date is yesterday:Subtract 1 day from the current date to get yesterday's date.

How do I know if a date is today?

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.

How can I get yesterday's date from a new 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.

How do you find out if two dates are the same?

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.


2 Answers

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
like image 141
Kapilrc Avatar answered Sep 18 '22 05:09

Kapilrc


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";
like image 22
Praveen Kumar Purushothaman Avatar answered Sep 22 '22 05:09

Praveen Kumar Purushothaman