JavaScript
Article

jQuery Get Later Date / Future Date

By Sam Deering

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

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.