JSFiddle - React, Tailwind, and code Playground
HTML
<div id="difference"></div>
JavaScript
// use milliseconds to see how many days (exactly) it has been, down to the millisecond
// number of milliseconds in a single day
var one_day_in_milliseconds = 1000*60*60*24; // 86400000ms
// date you want to compare
var compare = new Date("09/01/2018")
// todays date (per local machine)
var today = new Date();
// use Date.getTime() to get millisecond comparison difference
var difference = compare.getTime() - today.getTime();
// what to display
var message;
if ( difference > 0 ) {
// difference in days, ignoring partial days (no decimal)
var days = Math.floor( difference / one_day_in_milliseconds );
message = days + " day(s) is";
// 28 days is 4 weeks
if ( days <= 28 ) {
message += " within 4 weeks from now.";
} else {
message += " further than 4 weeks from now.";
}
} else {
//the compared date is in the past, automatically this isn't desirable
message = "The date being compared is not valid, it is in the past.";
}
// display message
document.getElementById('difference').innerHTML = message;