0

How can i check if input date is Today, Yesterday or just return the date if it's noone of them?

I was trying to do the following:

    const checkDate = (someDate) => {
      const today = new Date();
      let date = new Date(someDate);
      today.setHours(0);
      today.setMinutes(0);
      today.setSeconds(0);
      return date.getTime() === today.getTime() ? "Oggi" : date.getTime() === today.setDay(-1) ? "Ieri" : "Il" + date;
    }
    
    console.log(checkDate("December 17, 1995 03:24:00"));

Where "Oggi" is Today and "Ieri" is Yesterday..

Aalexander
  • 4,834
  • 3
  • 7
  • 30
Kasper Juner
  • 438
  • 11

6 Answers6

1

isToday:

const isToday = (date) => {
    const today = new Date()
    return date.getDate() === today.getDate() &&
        date.getMonth() === today.getMonth() &&
        date.getFullYear() === today.getFullYear();
};

isYesterday:

const isYesterday = (date) => {
    var yesterday= new Date();
    yesterday; //# => today
    date.setDate(date.getDate() - 1);
    yesterday; //# => yesterday

        return date.getDate() === yesterday.getDate() &&
            date.getMonth() === yesterday.getMonth() &&
            date.getFullYear() === yesterday.getFullYear();
}
0

Compare .toDateString to check if a Date() is the same day:

const checkDate = (someDate) => {

    // Create 'today' Date object
    const today = new Date();

    // Create 'someDate' Date object
    const date = new Date(someDate);

    // is Today
    if (date.toDateString() === today.toDateString()) {
        return 'today';
    }

    // Alter date to yesterday
    today.setDate(today.getDate() - 1);

    // is Yesterday
    if (date.toDateString() === today.toDateString()) {
        return 'yesterday';
    }

    // Fallback
    return 'fallback'
}

console.log(checkDate('1/4/2021'));  // today
console.log(checkDate('1/3/2021'));  // yesterday
console.log(checkDate('1/9/2021'));  // fallback
0stone0
  • 11,780
  • 2
  • 20
  • 35
0

The easiest and most probably the right way is to use some library. Try day.js - small, but features rich (similar to Moment).

To install:

npm install dayjs

Or CDN:

<script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script>

And you are ready to use all the set of features:

const someDate = new Date(); // plain old JS date
const now = dayjs(); // same (current date) but with day.js

// of course you can do: dayjs(someDate)

// yesterday: you check if someDate is current date - 1 day
const isYesterday = dayjs(someDate).isSame(dayjs().subtract(1, 'day'))

// today: just check if some date is equal to current date
const isToday = dayjs(someDate).isSame(dayjs()); // dayjs() return current date 

// want to get back to plain old JS date
conat plainOldJsDate = dayjs('2019-01-25').toDate(); 

You can find more awesome features like parsing, manipulating, formatting, etc. in official docs.

ikos23
  • 3,661
  • 9
  • 30
  • 47
0

The issue was that i missed to set mills and setDay was invalid, solved by using setDate(getDate() - 1) and by setting mills to 0

Here is the final solution

const checkDate = (someDate) => {
  let today = new Date();
  let date = new Date(someDate);
  date = resetHours(date);
  today = resetHours(today);
  return date.getTime() === today.getTime() ? "Oggi" : date.getTime() === today.setDate(today.getDate() - 1) ? "Ieri" : "Il" + date;
}

const resetHours = (date) => {
  date.setHours(0);
  date.setMinutes(0);
  date.setSeconds(0);
  date.setMilliseconds(0);
  return date;
}
Kasper Juner
  • 438
  • 11
0

You need to get the current date with a Date() object, then compare. The function receives strings in the form of mm/dd/yyyy.

function checkDate(date) {
  // get today
  var today = new Date();
  var dd = String(today.getDate()).padStart(2, '0');
  var mm = String(today.getMonth() + 1).padStart(2, '0');
  var yyyy = today.getFullYear();

  // parse into strings
  today = mm + '/' + dd + '/' + yyyy;
  yesterday = mm + '/' + (dd-1) + '/' + yyyy;
  
  if (today == date) {
    return 'today';
  }
  if (today == yesterday) {
    return 'yesterday';
  }
  else {
    return date;
  }
}

var date = '01/04/2021';
console.log(checkDate(date));
barhatsor
  • 1,347
  • 3
  • 17
0

const isSameDay = (a, b) => {
  return a.getFullYear() === b.getFullYear() &&
         a.getMonth() === b.getMonth() &&
         a.getDate()=== b.getDate();
}

const checkDate = (date) => {
  const today = new Date();
  const yesterday = new Date(Date.now() - 86400000); // 24 * 60 * 60 * 1000

  if (isSameDay(today, date)) {
    return "Today";
  } else if (isSameDay(yesterday, date)) {
    return "Yesterday";
  } else {
     return date.toDateString();
  }
}

console.log(checkDate(new Date())); // "Today"
console.log(checkDate(new Date(Date.now() - 86400000))); // "Yesterday"
console.log(checkDate(new Date(Date.now() - 86400000 * 3))); // "Fri Jan 01 2021"

Date documentation in MDN

prnsml
  • 1,454
  • 9
  • 16