0

I'm trying to get a conditional what with the date of tomorrow example let me know what if is tomorrow and is more than 1pm of tomorrow so do something.

I work with MomentJs, so if you have a solution with this that can help me, is appreciated.

Santiago
  • 114
  • 10
  • Tomorrow never comes… ;-). You might consider making a date for "today", then [*add 1 day*](https://stackoverflow.com/questions/9989382/how-can-i-add-1-day-to-current-date/9989458?r=SearchResults&s=4|76.7472#9989458) and set the time to 13:00:00 (`date.setHours(13,0,0,0)`). Then you can [*compare other Dates*](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) to see if they're before or after. – RobG Jun 10 '19 at 04:52

2 Answers2

1

In momentJS, you can get tomorrow by

const tomorrow  = moment().add(1, 'days');

If you want to get tomorrow 1.00 PM then

var tomorrow = moment('01:00 pm', "HH:mm a").add(1, 'days');

If you want to compare, then

var tomorrow = moment('01:00 pm', "HH:mm a").add(1, 'days');

var current = moment();

if(current < tomorrow)
  console.log('true');
else
  console.log('false');
Noor A Shuvo
  • 2,639
  • 3
  • 23
  • 48
1

To get a Date for 13:00 tomorrow, you can create a date for today, add a day, then set the time to 13:00.

Then you can directly compare it to other dates using comparison operators < and > (but you need to convert both dates to numbers to compare as == or ===).

E.g.

// return true if d0 is before d1, otherwise false
function isBefore(d0, d1) {
  return d0 < d1;
}

// Generate some sample dates
let now = new Date();
let d0 = new Date(now.setDate(now.getDate() + 1));
let d1 = new Date(now.setHours(10));
let d2 = new Date(now.setHours(15));

// Format output
function formatDate(d) {
  let options = {day:'2-digit', month:'short', year:'numeric', 'hour-12':false, hour:'2-digit',minute:'2-digit'};
  return d.toLocaleString('en-gb',options);
}

// Create a test date for 13:00 tomorrow
var testDate = new Date();
testDate.setDate(testDate.getDate() + 1);
testDate.setHours(13,0,0,0);

// Test it against sample dates
[d0,d1,d2].forEach(function(d) {
  console.log(formatDate(d) + ' before ' + formatDate(testDate) + '? ' + isBefore(d, testDate)); 
})
RobG
  • 142,382
  • 31
  • 172
  • 209