如何使用JavaScript将日期添加到当前日期?JavaScript是否有像.NET的AddDay()那样的内置函数?
当前回答
为管道运营商设计的解决方案:
const addDays = days => date => {
const result = new Date(date);
result.setDate(result.getDate() + days);
return result;
};
用法:
// Without the pipeline operator...
addDays(7)(new Date());
// And with the pipeline operator...
new Date() |> addDays(7);
如果您需要更多功能,我建议查看日期fns库。
其他回答
编辑:您可以这样做,而不是setTime()(或setHours()):
Date.prototype.addDays= function(d){
this.setDate(this.getDate() + d);
return this;
};
var tomorrow = new Date().addDays(1);
Old:
可以使用setHours()代替setTime():
Date.prototype.addDays= function(d){
this.setHours(this.getHours() + d * 24);
return this;
};
var tomorrow = new Date().addDays(1);
查看JSFiddle。。。
一些扩展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 {};
最简单的解决方案。
Date.prototype.addDays=函数(天){this.setDate(this.getDate()+parseInt(天));返回此;};//然后打电话var newDate=新日期().addDays(2)//+2天console.log(newDate);//或var newDate1=新日期().addDays(-2)//-2天console.log(newDate1);
短:
函数addDays(日期,数字){const newDate=新日期(日期);返回新日期(newDate.setDate(newDate.getDate()+number));}控制台日志({明天:addDays(新日期(),1)});
预付款:
函数addDays(日期,数字){const newDate=新日期(日期);返回新日期(newDate.setDate(Date.getDate()+number));}函数addMonths(日期,数字){const newDate=新日期(日期);返回新日期(newDate.setMonth(newDate.getMonth()+number));}函数addYears(日期,数字){const newDate=新日期(日期);返回新日期(newDate.setFullYear(newDate.getFullYear)+number);}函数getNewDate(dateTime){let date=新日期();let number=parseInt(dateTime.match(/\d+/)[0]);if(dateTime.indexOf('-')!=-1)number=(-number);如果(dateTime.indexOf('day')!=-1)date=addDays(日期,数字);否则如果(dateTime.indexOf('month')!=-1)date=addMonths(日期,数字);否则如果(dateTime.indexOf('year')!=-1)date=addYears(日期,数字);返回日期;}控制台日志({明天:获取新日期(“+1天”),昨天:getNewDate('-1day'),nextMonth:getNewDate(“+1个月”),nextYear:getNewDate(“+1年”),});
使用jperl提供的修复程序
我试图解决类似的问题,我更喜欢getTime方法,但有一些奇怪的基于时区的副作用。
ofc将“今天”替换为您需要的任何日期,并将时间也输入。关键是获取UTC时间,然后用毫秒来做加法,以避免这些副作用。
var now = new Date(Date.now());
var today = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate()));
const dayInMs = 86400000; //24 hours
const tomorrow = new Date(today.getTime() + dayInMs);