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.
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.
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');
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));
})