如何使用JavaScript将日期添加到当前日期?JavaScript是否有像.NET的AddDay()那样的内置函数?
当前回答
使用js-joda。它是一个很棒的javascript不可变日期和时间库。以下是其备忘单的摘录。
为今天增加17天
LocalDate.now().plusDays(17);
您还可以一次从多个操作构建所需的日期。
LocalDate.now()
.plusMonths(1)
.withDayOfMonth(1)
.minusDays(17);
Or:
var d = LocalDate.parse('2019-02-23');
d.minus(Period.ofMonths(3).plusDays(3)); // '2018-11-20'
其他回答
感谢Jason的回答,您的回答符合预期,这里是您的代码和AnthonyWJones的便捷格式的混合:
Date.prototype.addDays = function(days){
var ms = new Date().getTime() + (86400000 * days);
var added = new Date(ms);
return added;
}
一些扩展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 {};
对于不知道如何使其工作的每个人来说:有一个完整的工作代码,它并不完美,但你可以复制过去,它正在工作。
在InDesign中,在“Program Files\Adobe\Adobe InDesign 2021 \scripts\startup scripts”的启动脚本文件夹中创建.jsx。
您可以使用创意云中的Extendescript Toolkit CC制作并粘贴:
restart-design和jjmmyyyy+30应该在texte变量中。这将显示这样的日期jj/m/yyyy-idk,如何将其显示为2021 7月24日,而不是2021 7月24号,但对我来说已经足够了。
#targetengine 'usernameVariable'
function addVariables(openEvent)
{
var doc = openEvent.parent;
while ( doc.constructor.name != "Document" )
{
if ( doc.constructor.name == "Application" ){ return; }
doc = doc.parent;
}
// from http://stackoverflow.com/questions/563406/add-days-to-datetime
var someDate = new Date();
var numberOfDaysToAdd = 30;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();
var someFormattedDate = dd + '/'+ mm + '/'+ y;
createTextVariable(doc, "jjmmyyyy+30", someFormattedDate);
}
function createTextVariable(target, variableName, variableContents)
{
var usernameVariable = target.textVariables.itemByName(variableName);
if (!usernameVariable.isValid)
{
usernameVariable = target.textVariables.add();
usernameVariable.variableType = VariableTypes.CUSTOM_TEXT_TYPE;
usernameVariable.name = variableName;
}
usernameVariable.variableOptions.contents = variableContents;
}
app.addEventListener('afterOpen', addVariables);
为管道运营商设计的解决方案:
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库。
我总结了小时和天。。。
Date.prototype.addDays = function(days){
days = parseInt(days, 10)
this.setDate(this.getUTCDate() + days);
return this;
}
Date.prototype.addHours = function(hrs){
var hr = this.getUTCHours() + parseInt(hrs , 10);
while(hr > 24){
hr = hr - 24;
this.addDays(1);
}
this.setHours(hr);
return this;
}
推荐文章
- 在JavaScript中将JSON字符串解析为特定对象原型
- 将字符串“true”/“false”转换为布尔值
- 如何使用JavaScript代码获得浏览器宽度?
- event.preventDefault()函数在IE中无法工作
- indexOf()和search()的区别是什么?
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- React-Native:应用程序未注册错误
- LoDash:从对象属性数组中获取值数组
- src和dist文件夹的作用是什么?
- jQuery UI对话框-缺少关闭图标
- SQL Developer只返回日期,而不是时间。我怎么解决这个问题?
- 如何使用AngularJS获取url参数
- 将RGB转换为白色的RGBA
- 如何将“camelCase”转换为“Camel Case”?
- 我们可以在另一个JS文件中调用用一个JavaScript编写的函数吗?