如何使用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 {};
其他回答
//the_day is 2013-12-31
var 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"
这类函数有问题,我用parseInt()解决
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;
}
我已经用这种方法在一行中找到了正确的日期,以获得时间加上人们上面所说的一天。
((new Date()).setDate((new Date()).getDate()+1))
我只是想建立一个正常的(新日期()):
(new Date()).getDate()
> 21
使用上面的代码,我现在可以在(newDate())中的Date()中设置所有这些,并且它的行为正常。
(new Date(((new Date()).setDate((new Date()).getDate()+1)))).getDate()
> 22
或获取Date对象:
(new Date(((new Date()).setDate((new Date()).getDate()+1))))
您可以在此处创建自定义助手函数
function plusToDate(currentDate, unit, howMuch) {
var config = {
second: 1000, // 1000 miliseconds
minute: 60000,
hour: 3600000,
day: 86400000,
week: 604800000,
month: 2592000000, // Assuming 30 days in a month
year: 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);
顺便说一句,这是一个通用的解决方案,可以根据需要添加秒、分钟或天。
您可以使用JavaScript,不需要jQuery:
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;
推荐文章
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置
- 文档之间的区别。addEventListener和window。addEventListener?
- 如何检查动态附加的事件监听器是否存在?
- 前一个月的Python日期
- 如何写setTimeout与参数Coffeescript
- 将JavaScript字符串中的多个空格替换为单个空格
- JavaScript: override alert()
- 重置setTimeout
- 如何确保<select>表单字段被禁用时提交?
- jQuery有不聚焦的方法吗?
- 反应钩子-正确的方式清除超时和间隔