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


当前回答

Try

var someDate = new Date();
var duration = 2; //In Days
someDate.setTime(someDate.getTime() +  (duration * 24 * 60 * 60 * 1000));

使用setDate()添加日期并不能解决您的问题,请尝试在2月份添加一些天,如果您尝试在其中添加新的天,则不会产生您预期的结果。

其他回答

您可以在此处创建自定义助手函数

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

顺便说一句,这是一个通用的解决方案,可以根据需要添加秒、分钟或天。

最简单的解决方案。

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

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

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

我试图解决类似的问题,我更喜欢getTime方法,但有一些奇怪的基于时区的副作用。

ofc将“今天”替换为您需要的任何日期,并将时间也输入。关键是获取UTC时间,然后用毫秒来做加法,以避免这些副作用。

var now = new Date(Date.now());
var today = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate()));

const dayInMs = 86400000; //24 hours
const tomorrow = new Date(today.getTime() + dayInMs);    

为管道运营商设计的解决方案:

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库。