如何使用JavaScript将日期添加到当前日期?JavaScript是否有像.NET的AddDay()那样的内置函数?
当前回答
我使用的是:
new Date(dateObject.getTime() + amountOfDays * 24 * 60 * 60 * 1000)
节省时间的工作:
new Date(new Date(2014, 2, 29, 20, 0, 0).getTime() + 1 * 24 * 60 * 60 * 1000)
与新年一起工作:
new Date(new Date(2014, 11, 31, 20, 0, 0).getTime() + 1 * 24 * 60 * 60 * 1000)
它可以参数化:
function DateAdd(source, amount, step) {
var factor = 1;
if (step == "day") factor = 24 * 60 * 60 * 1000;
else if (step == "hour") factor = 60 * 60 * 1000;
...
new Date(source.getTime() + amount * factor);
}
其他回答
这类函数有问题,我用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(dateObject.getTime() + amountOfDays * 24 * 60 * 60 * 1000)
节省时间的工作:
new Date(new Date(2014, 2, 29, 20, 0, 0).getTime() + 1 * 24 * 60 * 60 * 1000)
与新年一起工作:
new Date(new Date(2014, 11, 31, 20, 0, 0).getTime() + 1 * 24 * 60 * 60 * 1000)
它可以参数化:
function DateAdd(source, amount, step) {
var factor = 1;
if (step == "day") factor = 24 * 60 * 60 * 1000;
else if (step == "hour") factor = 60 * 60 * 1000;
...
new Date(source.getTime() + amount * factor);
}
对于不知道如何使其工作的每个人来说:有一个完整的工作代码,它并不完美,但你可以复制过去,它正在工作。
在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);
已缩小2.39KB。一个文件。https://github.com/rhroyston/clock-js
console.log(clock.wwhat.wayday(clock.now+clock.unit.days))//“星期三”console.log(clock.wwhat.wayday(clock.now+(clock.unit.days*2))//“星期四”console.log(clock.wwhat.wayday(clock.now+(clock.unit.days*3))//“星期五”<script src=“https://raw.githubusercontent.com/rhroyston/clock-js/master/clock.min.js“></script>
Try
var someDate = new Date();
var duration = 2; //In Days
someDate.setTime(someDate.getTime() + (duration * 24 * 60 * 60 * 1000));
使用setDate()添加日期并不能解决您的问题,请尝试在2月份添加一些天,如果您尝试在其中添加新的天,则不会产生您预期的结果。