如何使用JavaScript将日期添加到当前日期?JavaScript是否有像.NET的AddDay()那样的内置函数?


当前回答

对于不知道如何使其工作的每个人来说:有一个完整的工作代码,它并不完美,但你可以复制过去,它正在工作。

在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);

其他回答

最简单的解决方案。

Date.prototype.addDays=函数(天){this.setDate(this.getDate()+parseInt(天));返回此;};//然后打电话var newDate=新日期().addDays(2)//+2天console.log(newDate);//或var newDate1=新日期().addDays(-2)//-2天console.log(newDate1);

如果可以,请使用moment.js。JavaScript没有很好的原生日期/时间方法。以下是Moment语法示例:

var nextWeek=moment().add(7,'天');警报(下一周);<script src=“https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment-with-locales.min.js“></script>

参考:http://momentjs.com/docs/#/manipulating/add/

我使用的是:

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);
}

我昨晚创建了这些扩展:可以传递正值或负值;

例子:

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;
}
    //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"