如何使用JavaScript将日期添加到当前日期?JavaScript是否有像.NET的AddDay()那样的内置函数?
当前回答
一些扩展Date的实现https://gist.github.com/netstart/c92e09730f3675ba8fb33be48520a86d
/**
* just import, like
*
* import './../shared/utils/date.prototype.extendions.ts';
*/
declare global {
interface Date {
addDays(days: number, useThis?: boolean): Date;
addSeconds(seconds: number): Date;
addMinutes(minutes: number): Date;
addHours(hours: number): Date;
addMonths(months: number): Date;
isToday(): boolean;
clone(): Date;
isAnotherMonth(date: Date): boolean;
isWeekend(): boolean;
isSameDate(date: Date): boolean;
getStringDate(): string;
}
}
Date.prototype.addDays = function(days: number): Date {
if (!days) {
return this;
}
this.setDate(this.getDate() + days);
return this;
};
Date.prototype.addSeconds = function(seconds: number) {
let value = this.valueOf();
value += 1000 * seconds;
return new Date(value);
};
Date.prototype.addMinutes = function(minutes: number) {
let value = this.valueOf();
value += 60000 * minutes;
return new Date(value);
};
Date.prototype.addHours = function(hours: number) {
let value = this.valueOf();
value += 3600000 * hours;
return new Date(value);
};
Date.prototype.addMonths = function(months: number) {
const value = new Date(this.valueOf());
let mo = this.getMonth();
let yr = this.getYear();
mo = (mo + months) % 12;
if (0 > mo) {
yr += (this.getMonth() + months - mo - 12) / 12;
mo += 12;
} else {
yr += ((this.getMonth() + months - mo) / 12);
}
value.setMonth(mo);
value.setFullYear(yr);
return value;
};
Date.prototype.isToday = function(): boolean {
const today = new Date();
return this.isSameDate(today);
};
Date.prototype.clone = function(): Date {
return new Date(+this);
};
Date.prototype.isAnotherMonth = function(date: Date): boolean {
return date && this.getMonth() !== date.getMonth();
};
Date.prototype.isWeekend = function(): boolean {
return this.getDay() === 0 || this.getDay() === 6;
};
Date.prototype.isSameDate = function(date: Date): boolean {
return date && this.getFullYear() === date.getFullYear() && this.getMonth() === date.getMonth() && this.getDate() === date.getDate();
};
Date.prototype.getStringDate = function(): string {
// Month names in Brazilian Portuguese
const monthNames = ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'];
// Month names in English
// let monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const today = new Date();
if (this.getMonth() === today.getMonth() && this.getDay() === today.getDay()) {
return 'Hoje';
// return "Today";
} else if (this.getMonth() === today.getMonth() && this.getDay() === today.getDay() + 1) {
return 'Amanhã';
// return "Tomorrow";
} else if (this.getMonth() === today.getMonth() && this.getDay() === today.getDay() - 1) {
return 'Ontem';
// return "Yesterday";
} else {
return this.getDay() + ' de ' + this.monthNames[this.getMonth()] + ' de ' + this.getFullYear();
// return this.monthNames[this.getMonth()] + ' ' + this.getDay() + ', ' + this.getFullYear();
}
};
export {};
其他回答
有一个setDate和一个getDate方法,允许您执行以下操作:
var newDate = aDate.setDate(aDate.getDate() + numberOfDays);
如果您想减去天数并将日期格式化为可读格式,则应考虑创建一个自定义DateHelper对象,其外观如下:
var日期助手={addDays:函数(aDate,numberOfDays){aDate.setDate(aDate.getDate()+天数);//添加天数return aDate;//返回日期},格式:函数格式(日期){返回[(“0”+date.getDate()).slice(-2),//获取日期并用零填充(“0”+(date.getMonth()+1)).slice(-2),//获取月份并用零填充date.getFullYear()//获取全年].ejoin('/');//把碎片粘在一起}}//有了这个助手,您现在只需使用一行可读代码即可:// ---------------------------------------------------------------------// 1. 获取当前日期// 2. 增加20天// 3. 格式化它// 4. 输出它// ---------------------------------------------------------------------document.body.innerHTML=DateHelper.format(DateHelper.addDays(new Date(),20));
(另见本Fiddle)
setDate()的mozilla文档并不表示它将处理月末场景。看见https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date
设置日期()
根据当地时间设置指定日期的月份日期(1-31)。
这就是我在需要添加天数时使用setTime()的原因。
同样的答案是:如何将天数添加到今天的日期?
function DaysOfMonth(nYear, nMonth) {
switch (nMonth) {
case 0: // January
return 31; break;
case 1: // February
if ((nYear % 4) == 0) {
return 29;
}
else {
return 28;
};
break;
case 2: // March
return 31; break;
case 3: // April
return 30; break;
case 4: // May
return 31; break;
case 5: // June
return 30; break;
case 6: // July
return 31; break;
case 7: // August
return 31; break;
case 8: // September
return 30; break;
case 9: // October
return 31; break;
case 10: // November
return 30; break;
case 11: // December
return 31; break;
}
};
function SkipDate(dDate, skipDays) {
var nYear = dDate.getFullYear();
var nMonth = dDate.getMonth();
var nDate = dDate.getDate();
var remainDays = skipDays;
var dRunDate = dDate;
while (remainDays > 0) {
remainDays_month = DaysOfMonth(nYear, nMonth) - nDate;
if (remainDays > remainDays_month) {
remainDays = remainDays - remainDays_month - 1;
nDate = 1;
if (nMonth < 11) { nMonth = nMonth + 1; }
else {
nMonth = 0;
nYear = nYear + 1;
};
}
else {
nDate = nDate + remainDays;
remainDays = 0;
};
dRunDate = Date(nYear, nMonth, nDate);
}
return new Date(nYear, nMonth, nDate);
};
如果可以,请使用moment.js。JavaScript没有很好的原生日期/时间方法。以下是Moment语法示例:
var nextWeek=moment().add(7,'天');警报(下一周);<script src=“https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment-with-locales.min.js“></script>
参考:http://momentjs.com/docs/#/manipulating/add/
不使用第二个变量,您可以用接下来的x天替换7:
let d=new Date(new Date().getTime() + (7 * 24 * 60 * 60 * 1000));