var d = new Date();
document.write('Today is: ' + d.toLocaleString());
d.setDate(d.getDate() - 5);
document.write('<br>5 days ago was: ' + d.toLocaleString());
var newDate = Date.now() + -5*24*3600*1000; // date 5 days ago in milliseconds UTCnew Date(newDate).toUTCString(); // or .toISOString(), BUT NOT toString
B.您以“现在”以外的UTC基准日期开始,使用Date.UTC()
newDate = new Date(Date.UTC(2015, 3, 1)).getTime() + -5*24*3600000;new Date(newDate).toUTCString(); // or .toISOString BUT NOT toString
function addDays(date, days) {return new Date(date.getFullYear(),date.getMonth(),date.getDate() + days,date.getHours(),date.getMinutes(),date.getSeconds(),date.getMilliseconds());}
function addDays( date, days ) {var dateInMs = date.setDate(date.getDate() - days);return new Date(dateInMs);}
var date_from = new Date();var date_to = addDays( new Date(), parseInt(days) );
var CurrDate = new Date(); // Current Datevar numberOfDays = 5;var days = CurrDate.setDate(CurrDate.getDate() + numberOfDays);alert(days); // It will print 5 days before today
对于PHP,
$date = date('Y-m-d', strtotime("-5 days")); // it shows 5 days before today.echo $date;
curDate = new Date(); // Took current date as an exampleprvDate = new Date(0); // Date set to epoch 0prvDate.setUTCMilliseconds((curDate - (5 * 24 * 60 * 60 * 1000))); //Set epoch time
var d = new Date();
document.write('Today is: ' + d.toLocaleString());
d.setDate(d.getDate() - 31);
document.write('<br>5 days ago was: ' + d.toLocaleString());
function addSubstractDays(date: Date, numberofDays: number): Date {let d = new Date(date);return new Date(d.getFullYear(),d.getMonth(),(d.getDate() + numberofDays));}
function addDays (date, daysToAdd) {var _24HoursInMilliseconds = 86400000;return new Date(date.getTime() + daysToAdd * _24HoursInMilliseconds);};
var now = new Date();
var yesterday = addDays(now, - 1);
var tomorrow = addDays(now, 1);
var numberOfDays = 10;//number of days need to deducted or addedvar date = "01-01-2018"// date need to changevar dt = new Date(parseInt(date.substring(6), 10), // YearparseInt(date.substring(3,5), 10) - 1, // Month (0-11)parseInt(date.substring(0,2), 10));var new_dt = dt.setMilliseconds(dt.getMilliseconds() - numberOfDays*24*60*60*1000);new_dt = new Date(new_dt);var changed_date = new_dt.getDate()+"-"+(new_dt.getMonth()+1)+"-"+new_dt.getFullYear();
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. Subtract 5 days// 3. Format it// 4. Output it// ---------------------------------------------------------------------document.body.innerHTML = DateHelper.format(DateHelper.addDays(new Date(), -5));
function getXDaysBeforeDate(referenceDate, x) {return moment(referenceDate).subtract(x , 'day').format('MMMM Do YYYY, h:mm:ss a');}
var yourDate = new Date(); // let's say todayvar valueOfX = 7; // let's say 7 days before
console.log(getXDaysBeforeDate(yourDate, valueOfX));
var date = new Date();var day=date.getDate();var month=date.getMonth() + 1;var year=date.getFullYear();var startDate=day+"/"+month+"/"+year;var dayBeforeNineDays=moment().subtract(10, 'days').format('DD/MM/YYYY');startDate=dayBeforeNineDays;var endDate=day+"/"+month+"/"+year;
//pastvar fiveDaysAgo = new Date(new Date().setDate(new Date().getDate() - 5));//futurevar fiveDaysInTheFuture = new Date(new Date().setDate(new Date().getDate() + 5));
从特定日期起5天
var pastDate = new Date('2019-12-12T00:00:00');
//pastvar fiveDaysAgo = new Date(new Date().setDate(pastDate.getDate() - 5));//futurevar fiveDaysInTheFuture = new Date(new Date().setDate(pastDate.getDate() + 5));
我写了一个你可以使用的函数。
function AddOrSubractDays(startingDate, number, add) {if (add) {return new Date(new Date().setDate(startingDate.getDate() + number));} else {return new Date(new Date().setDate(startingDate.getDate() - number));}}
console.log('Today : ' + new Date());console.log('Future : ' + AddOrSubractDays(new Date(), 5, true));console.log('Past : ' + AddOrSubractDays(new Date(), 5, false));