Skip to main content

jQuery Get Later Date / Future Date

By Sam Deering
JavaScript
Share:

Here is how you might get a a future date using jQuery. The future date is calculated based on the current date for example days later than the current date. If you are unsure, here is how you can get the current date.

Check if is future date using jQuery

function isFutureDate()
{
    var date=new Date();
    date.setFullYear(2020,1,1);
    var _now=new Date();
    if(date.getTime()>_now.getTime())
        {
      // date is future
        }
}

Check if is a 4 digit year

This functions checks if the date has 2 digit days and months and 4 digit year.

function twodigits(digits) {
    return (digits > 9) ? digits : '0' + digits;
}

The full code

var fullDate = new Date();
var twoDigitDays = twodigits(fullDate.getDate());
var twoDigitMonth = twodigits(fullDate.getMonth() + 1);

var departDate = twoDigitDays + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
console.log(departDate);

fullDate.setDate(fullDate.getDate()+7);
var twoDigitDays = twodigits(fullDate.getDate());
var twoDigitMonth = twodigits(fullDate.getMonth() + 1);

var returnDate = twoDigitDays + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
console.log(returnDate);

Alternate Example

var currentDate = new Date(),
currentMonth = currentDate.getMonth() + 1,
lastDayOfMonth = new Date(currentDate.getFullYear(), (currentDate.getMonth() - 1), 0).getDate(),
departureDate = futureDateDays(14),
depDate = departureDate.split('/'),
departureDateMonth = depDate[1];
if (departureDateMonth != currentMonth) {
	departureDate = leadingZero(currentMonth) + '/' + leadingZero(lastDayOfMonth) +'/'+ depDate[2];
}
console.log(departureDate);
//quick fix convert back to date using string format mm/dd/yyyy
var validDate = new Date(departureDate);
//then back to string
departureDate = leadingZero(validDate.getDate()) + '/' + leadingZero(validDate.getMonth()+1) +'/'+ validDate.getFullYear();

//Output: 
//09/31/2011
//Date {Sat Oct 01 2011 00:00:00 GMT+1000}
//01/10/2011

Tags

jQuery

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

Integromat Tower Ad