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


当前回答

我的测试示例可以在日期对象的同一实例中执行减号。

Date.prototype.reset=函数(){let newDate=新日期(this.timeStamp)this.setFullYear(newDate.getFullYear)this.setMonth(newDate.getMonth())this.setDate(newDate.getDate())this.setHours(newDate.getHours())this.set分钟(newDate.getMinutes())this.setSeconds(newDate.getSeconds())this.set毫秒(newDate.getMilliseconds())}Date.prototype.addDays=函数(天){this.timeStamp=此[Symbol.toPrimitive]('编号')let daysInMiliseconds=(天*(1000*60*60*24))this.timeStamp=this.timeStamp+天毫秒this.reset()}Date.prototype.minusDays=函数(天){this.timeStamp=此[Symbol.toPrimitive]('编号')let daysInMiliseconds=(天*(1000*60*60*24))如果(daysInMiliseconds<=this.timeStamp){this.timeStamp=this.timeStamp-天毫秒this.reset()}}var temp=新日期(Date.now())//从现在开始console.log(temp.toDateString())临时添加天数(31)console.log(temp.toDateString())温度-天(5)console.log(temp.toDateString())

其他回答

这些答案让我感到困惑,我更喜欢:

var ms = new Date().getTime() + 86400000;
var tomorrow = new Date(ms);

getTime()给出了自1970年以来的毫秒数,86400000是一天中的毫秒数。因此,ms包含所需日期的毫秒。

使用毫秒构造函数可以得到所需的日期对象。

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

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

Try

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

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

我的测试示例可以在日期对象的同一实例中执行减号。

Date.prototype.reset=函数(){let newDate=新日期(this.timeStamp)this.setFullYear(newDate.getFullYear)this.setMonth(newDate.getMonth())this.setDate(newDate.getDate())this.setHours(newDate.getHours())this.set分钟(newDate.getMinutes())this.setSeconds(newDate.getSeconds())this.set毫秒(newDate.getMilliseconds())}Date.prototype.addDays=函数(天){this.timeStamp=此[Symbol.toPrimitive]('编号')let daysInMiliseconds=(天*(1000*60*60*24))this.timeStamp=this.timeStamp+天毫秒this.reset()}Date.prototype.minusDays=函数(天){this.timeStamp=此[Symbol.toPrimitive]('编号')let daysInMiliseconds=(天*(1000*60*60*24))如果(daysInMiliseconds<=this.timeStamp){this.timeStamp=this.timeStamp-天毫秒this.reset()}}var temp=新日期(Date.now())//从现在开始console.log(temp.toDateString())临时添加天数(31)console.log(temp.toDateString())温度-天(5)console.log(temp.toDateString())

为什么这么复杂?

假设您将要添加的天数存储在一个名为days_to_add的变量中。

那么这个简短的应该做到:

calc_date = new Date(Date.now() +(days_to_add * 86400000));

使用Date.now(),您可以获得实际的unix时间戳(以毫秒为单位),然后添加您想要添加的天数。一天为24h60min60s*1000ms=86400000 ms或864E5。