vuvu
vuvu

Reputation: 5308

How to calc if a given date is the date of today or or the date of yesterday?

I want to calculate if a given date is from today or yesterday. At the moment I can only check if it is 24h or 48h old. How can I check the date?

var date_given = new Date(Date.parse(item.date));

var today = new Date();
var diff = ( Date.parse(today) - Date.parse(date_given) ) /(1000*24*60*60);

if (diff < 1) {
    return "today";
} else if (diff < 2) {
    return "yesterday";
} 

Upvotes: 1

Views: 110

Answers (3)

DvS
DvS

Reputation: 1095

You can use these functions to add any number of days (negative numbers as well) and to determine if the date portions of a date are equal:

var msInDay = 86400000;

function addDays(date, add) {
    return new Date(date.valueOf() + (msInDay * add));
}

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

This will allow you to get yesterday:

var today = new Date();
var yesterday = addDays(today, -1);

Now compare:

var otherDate = addDays(today, -1);
var isYesterday = isSameDay(otherDate, yesterday);
var isToday = isSameDay(otherDate, today);

Upvotes: 2

Anand Gupta
Anand Gupta

Reputation: 5700

If you only want to check if given date is today's or yesterday then just try this function. You don't need to write a very long piece of code:

function checkTodayYesterday(givenDate) {
    var today = new Date();
    var yesterday = new Date();
    yesterday.setDate(yesterday.getDate() - 1);
    if(today.toDateString() === givenDate.toDateString()) {
        console.log('givenDate is today');
    } else if(yesterday.toDateString() === givenDate.toDateString()) {
        console.log('givenDate is yesterday');
    } else  {
        console.log('givenDate is other than today or yesterday');
    }
}

Happy coding! :)

Upvotes: 1

cl3m
cl3m

Reputation: 2801

You could do it old school like this :

var dateTime = function(args) {
  args = args || [0,0,0,0]
  var d = new Date(Date.now())
  d.setUTCHours(args[0])
  d.setUTCMinutes(args[1])
  d.setUTCSeconds(args[2])
  d.setUTCMilliseconds(args[3])
  return d;
}

var isItTodayOrYesterday = function(given) {
  var start = dateTime(),
      end   = dateTime([23,59,59,999])
  return given < start ? 'yesterday' : given < end ? 'today' : "its the futur man"
}

var futur = new Date('December 17, 2018 03:24:00'),
    today = new Date(),
    past  = new Date('December 17, 1995 03:24:00');

console.log(isItTodayOrYesterday(past)) // yesterday
console.log(isItTodayOrYesterday(today)) // today
console.log(isItTodayOrYesterday(futur)) // its the futur man

See this fiddle

Edit

Per your comment, here is a version that takes care of the yesterday case:

var dateTime = function(args) {
  var time = (args && args.time) ? args.time : [0,0,0,0],
      date = new Date((args && args.date) || Date.now())

  date.setUTCHours(time[0])
  date.setUTCMinutes(time[1])
  date.setUTCSeconds(time[2])
  date.setUTCMilliseconds(time[3])
  return date;
}

console.clear()

var isItTodayOrYesterday = function(given) {
  var start     = dateTime(),
      yesterday = dateTime({date: new Date(new Date().valueOf() - 3600 * 24 * 1000)}),
      end       = dateTime({time: [23,59,59,999]})

  return given < yesterday ? "we're stuck in the past" 
    : given < start ? 'yesterday' 
      : given < end ? 'today' 
        : "its the futur man"
}

var futur       = new Date('December 17, 2018 03:24:00'),
    today       = new Date(),
    past        = new Date('December 17, 1995 03:24:00'),
    yesterday   = dateTime({
      date: new Date(new Date().valueOf() - 3600 * 24 * 1000),
      time: [12,21,30,0]
    });

console.log(isItTodayOrYesterday(past)) // "we're stuck in the past"
console.log(isItTodayOrYesterday(yesterday)) // yesterday
console.log(isItTodayOrYesterday(today)) // today
console.log(isItTodayOrYesterday(futur)) // its the futur man

See the updated fiddle

Upvotes: 2

Related Questions