1

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";
} 
Anand Gupta
  • 5,640
  • 3
  • 27
  • 37
vuvu
  • 4,886
  • 12
  • 50
  • 73
  • 3
    look at http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Phoenix Mar 30 '16 at 09:30
  • 2
    investigate using moment.js - very useful to implement data and time functions – gavgrif Mar 30 '16 at 09:31
  • 1
    @Phoenix yesterday can be a second or 24h ago, how to check that? – vuvu Mar 30 '16 at 09:55
  • You will get every date as long using `date.getTime()` so making the diff between these two longs will give you an other long that you just have to convert on second or hours to know if it was a second or 24h ago – Phoenix Mar 30 '16 at 10:38
  • @Phoenix that is true, but the how to know if 23h ago or 1 second ago is still today or already yesterday! – vuvu Mar 30 '16 at 10:53
  • convert it to a full date (using momentjs or anything else) and check the day if it is today or yesterday ... – Phoenix Mar 30 '16 at 10:56

3 Answers3

2

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);
DvS
  • 1,025
  • 6
  • 11
  • [getDay()](http://www.w3schools.com/jsref/jsref_getday.asp) return the weekday-index not the day of the date, if two date both have tuesday with weekday-index==2 you cannot distinguish these, better use getDate() – vuvu Mar 30 '16 at 13:22
  • Yes, thanks @vuvu. Silly mistake - have corrected answer – DvS Mar 31 '16 at 09:27
2

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

cl3m
  • 2,791
  • 19
  • 21
1

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! :)

Anand Gupta
  • 5,640
  • 3
  • 27
  • 37