如何使用JavaScript将日期添加到当前日期?JavaScript是否有像.NET的AddDay()那样的内置函数?
当前回答
我正在使用以下解决方案。
var msInDay = 86400000;
var daysToAdd = 5;
var now = new Date();
var milliseconds = now.getTime();
var newMillisecods = milliseconds + msInDay * daysToAdd;
var newDate = new Date(newMillisecods);
//or now.setTime(newMillisecods);
Date有一个接受int的构造函数。此参数表示1970年1月1日之前/之后的总毫秒数。它还有一个方法setTime,该方法在不创建新的Date对象的情况下执行同样的操作。
我们在这里做的是将天数转换为毫秒,并将该值添加到getTime提供的值中。最后,我们将结果提供给Date(毫秒)构造函数或setTime(毫秒)方法。
其他回答
我知道,但有时我喜欢这样:
function addDays(days) {
return new Date(Date.now() + 864e5 * days);
}
var today = new Date();
var tomorrow = new Date();
tomorrow.setDate(today.getDate()+1);
小心,因为这可能很棘手。当设置明天时,它仅在当前值与今天的年份和月份匹配时有效。然而,通常将日期设置为“32”这样的数字仍然可以很好地将其移动到下一个月。
一些扩展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 {};
感谢Jason的回答,您的回答符合预期,这里是您的代码和AnthonyWJones的便捷格式的混合:
Date.prototype.addDays = function(days){
var ms = new Date().getTime() + (86400000 * days);
var added = new Date(ms);
return added;
}
我昨晚创建了这些扩展:可以传递正值或负值;
例子:
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;
}
else
yr += ((this.getMonth() + num - mo) / 12);
value.setMonth(mo);
value.setYear(yr);
return value;
}
推荐文章
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?