Date.prototype.addDays = function(days) {var date = new Date(this.valueOf());date.setDate(date.getDate() + days);return date;}
var date = new Date();
console.log(date.addDays(5));
var someDate = new Date();var expirationDate = someDate.addDays(10);var previous = someDate.addDays(-5);
Date.prototype.addDays = function (num) {var value = this.valueOf();value += 86400000 * num;return new Date(value);}
Date.prototype.addSeconds = function (num) {var value = this.valueOf();value += 1000 * num;return new Date(value);}
Date.prototype.addMinutes = function (num) {var value = this.valueOf();value += 60000 * num;return new Date(value);}
Date.prototype.addHours = function (num) {var value = this.valueOf();value += 3600000 * num;return new Date(value);}
Date.prototype.addMonths = function (num) {var value = new Date(this.valueOf());
var mo = this.getMonth();var yr = this.getYear();
mo = (mo + num) % 12;if (0 > mo) {yr += (this.getMonth() + num - mo - 12) / 12;mo += 12;}elseyr += ((this.getMonth() + num - mo) / 12);
value.setMonth(mo);value.setYear(yr);return value;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><table><tbody><tr><th colspan="4">DST Dates</th></tr><tr><th>Input</th><th>+1 Day</th><th>+1 Day Fail</th><th>+1 Day DST Fail</th></tr><tr><td>03/10/2013</td></tr><tr><td>11/03/2013</td></tr><tr><td>03/09/2014</td></tr><tr><td>11/02/2014</td></tr><tr><td>03/08/2015</td></tr><tr><td>11/01/2015</td></tr><tr><th colspan="4">2013</th></tr><tr><th>Input</th><th>+1 Day</th><th>+1 Day Fail</th><th>+1 Day DST Fail</th></tr><tr><td>01/01/2013</td></tr><tr><td>02/01/2013</td></tr><tr><td>03/01/2013</td></tr><tr><td>04/01/2013</td></tr><tr><td>05/01/2013</td></tr><tr><td>06/01/2013</td></tr><tr><td>07/01/2013</td></tr><tr><td>08/01/2013</td></tr><tr><td>09/01/2013</td></tr><tr><td>10/01/2013</td></tr><tr><td>11/01/2013</td></tr><tr><td>12/01/2013</td></tr><tr><th colspan="4">2014</th></tr><tr><th>Input</th><th>+1 Day</th><th>+1 Day Fail</th><th>+1 Day DST Fail</th></tr><tr><td>01/01/2014</td></tr><tr><td>02/01/2014</td></tr><tr><td>03/01/2014</td></tr><tr><td>04/01/2014</td></tr><tr><td>05/01/2014</td></tr><tr><td>06/01/2014</td></tr><tr><td>07/01/2014</td></tr><tr><td>08/01/2014</td></tr><tr><td>09/01/2014</td></tr><tr><td>10/01/2014</td></tr><tr><td>11/01/2014</td></tr><tr><td>12/01/2014</td></tr><tr><th colspan="4">2015</th></tr><tr><th>Input</th><th>+1 Day</th><th>+1 Day Fail</th><th>+1 Day DST Fail</th></tr><tr><td>01/01/2015</td></tr><tr><td>02/01/2015</td></tr><tr><td>03/01/2015</td></tr><tr><td>04/01/2015</td></tr><tr><td>05/01/2015</td></tr><tr><td>06/01/2015</td></tr><tr><td>07/01/2015</td></tr><tr><td>08/01/2015</td></tr><tr><td>09/01/2015</td></tr><tr><td>10/01/2015</td></tr><tr><td>11/01/2015</td></tr><tr><td>12/01/2015</td></tr></tbody></table>
//the_day is 2013-12-31var the_day = Date.UTC(2013, 11, 31);// Now, the_day will be "1388448000000" in UTC+8;var the_next_day = new Date(the_day + 24 * 60 * 60 * 1000);// Now, the_next_day will be "Wed Jan 01 2014 08:00:00 GMT+0800"
var days = 2;var newDate = new Date(Date.now() + days * 24*60*60*1000);
document.write('Today: <em>');document.write(new Date());document.write('</em><br/> New: <strong>');document.write(newDate);
Date.prototype.addDays = function(days) {this.setDate(this.getDate() + parseInt(days));return this;};
// and then call
var newDate = new Date().addDays(2); //+2 daysconsole.log(newDate);
// or
var newDate1 = new Date().addDays(-2); //-2 daysconsole.log(newDate1);
// Curried, so that I can create helper functions like `add1Day`const addDays = num => date => {// Make a working copy so we don't mutate the supplied date.const d = new Date(date);
d.setUTCDate(d.getUTCDate() + num);
return d;}
function plusToDate(currentDate, unit, howMuch) {
var config = {second: 1000, // 1000 milisecondsminute: 60000,hour: 3600000,day: 86400000,week: 604800000,month: 2592000000, // Assuming 30 days in a monthyear: 31536000000 // Assuming 365 days in year};
var now = new Date(currentDate);
return new Date(now + config[unit] * howMuch);}
var today = new Date();var theDayAfterTommorow = plusToDate(today, 'day', 2);
function addDays(n){var t = new Date();t.setDate(t.getDate() + n);var month = "0"+(t.getMonth()+1);var date = "0"+t.getDate();month = month.slice(-2);date = date.slice(-2);var date = date +"/"+month +"/"+t.getFullYear();alert(date);}
addDays(5);
var DateHelper = {addDays : function(aDate, numberOfDays) {aDate.setDate(aDate.getDate() + numberOfDays); // Add numberOfDaysreturn aDate; // Return the date},format : function format(date) {return [("0" + date.getDate()).slice(-2), // Get day and pad it with zeroes("0" + (date.getMonth()+1)).slice(-2), // Get month and pad it with zeroesdate.getFullYear() // Get full year].join('/'); // Glue the pieces together}}
// With this helper, you can now just use one line of readable code to :// ---------------------------------------------------------------------// 1. Get the current date// 2. Add 20 days// 3. Format it// 4. Output it// ---------------------------------------------------------------------document.body.innerHTML = DateHelper.format(DateHelper.addDays(new Date(), 20));
var someDate = new Date();var numberOfDaysToAdd = 6;someDate.setDate(someDate.getDate() + numberOfDaysToAdd);Formatting to dd/mm/yyyy :
var dd = someDate.getDate();var mm = someDate.getMonth() + 1;var y = someDate.getFullYear();
var someFormattedDate = dd + '/'+ mm + '/'+ y;
class MyCustomDate extends Date {
addDays(days) {const date = new MyCustomDate(this.valueOf());date.setDate(date.getDate() + days);return date;}
}
const today = new MyCustomDate();
const nextWeek = today.addDays(7)
console.log(nextWeek)
const days = 15;// Date.now() gives the epoch date value (in milliseconds) of current datenextDate = new Date( Date.now() + days * 24 * 60 * 60 * 1000)
// To add Daysvar d = new Date();d.setDate(d.getDate() + 5);
// To add Monthsvar m = new Date();m.setMonth(m.getMonth() + 5);
// To add Yearsvar y = new Date();y.setFullYear(y.getFullYear() + 5);
Date.prototype.addDays = function(dias) {
var date = new Date(this.valueOf());date.setDate(parseInt(date.getDate()) + parseInt(dias));return date;}
Date.prototype.addMonths = function(months) {var date = new Date(this.valueOf());date.setMonth(parseInt(date.getMonth()) + parseInt(months));return date;}
Date.prototype.addYears = function(years) {var date = new Date(this.valueOf());date.setFullYear(parseInt(date.getFullYear()) + parseInt(years));return date;}
var now = new Date(Date.now());var today = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate()));
const dayInMs = 86400000; //24 hoursconst tomorrow = new Date(today.getTime() + dayInMs);
function addDays(date, number) {const newDate = new Date(date);return new Date(newDate.setDate(newDate.getDate() + number));}
console.log({tomorrow: addDays(new Date(), 1)});
预付款:
function addDays(date, number) {const newDate = new Date(date);return new Date(newDate.setDate(date.getDate() + number));}
function addMonths(date, number) {const newDate = new Date(date);return new Date(newDate.setMonth(newDate.getMonth() + number));}
function addYears(date, number) {const newDate = new Date(date);return new Date(newDate.setFullYear(newDate.getFullYear() + number));}
function getNewDate(dateTime) {let date = new Date();let number = parseInt(dateTime.match(/\d+/)[0]);
if (dateTime.indexOf('-') != -1)number = (-number);
if (dateTime.indexOf('day') != -1)date = addDays(date, number);else if (dateTime.indexOf('month') != -1)date = addMonths(date, number);else if (dateTime.indexOf('year') != -1)date = addYears(date, number);
return date;}
console.log({tomorrow: getNewDate('+1day'),yesterday: getNewDate('-1day'),nextMonth: getNewDate('+1month'),nextYear: getNewDate('+1year'),});