Skip to content Skip to sidebar Skip to footer

Setting Date X Days From Today

We are setting up a Google Trusted Store and need to populate the following span id field ORDER_EST_SHIP_DATE with today's date plus 2 days: OR

Solution 1:

JavaScript date has a build in method:

var date = newDate();
date.setDate(date.getDate() + 2/*days*/ );
var dateString = date.toISOString().slice(0, 10);
document.getElementById('gts-o-est-ship-date').innerHTML = dateString;
<divid="gts-o-est-ship-date"></div>

Documentation on toISOString

Solution 2:

First convert the date to a timestamp in microseconds using Date.parse:

Than add two and seven days to it in microseconds, convert back to human readable date.

var timeStamp = Date.parse("2015-02-25");

//add two days, in microseconds = 60*60*24*2*1000varTwoDays = 172800000;
varSevenDays = 604800000;

functionconvertToDate(timeStamp)
{
  var date = newDate(timeStamp);
  return date.toJSON().split("T")[0];
}

document.write(convertToDate(timeStamp+TwoDays)); //show two days in futuredocument.write("<br />");
document.write(convertToDate(timeStamp+SevenDays)); //show seven days in future

This returns a UTC date. So you may need to convert this to the users locale. It's important that you pick a base times for all your time (UTC for example) and convert it to the users locale as a last step.

Post a Comment for "Setting Date X Days From Today"