【问题标题】:How do I calculate the date in JavaScript, to get the same day in month + some month (variable)如何在 JavaScript 中计算日期,以获得月份中的同一天 + 某个月份(变量)
【发布时间】:2020-02-28 14:19:51
【问题描述】:

有谁知道如何始终将日期设置为当月的第 28 天,或者如果日期已超过当月的第 28 天,则为下个月的第 28 天,然后使用变量(月数)计算新日期。 这就是它在 .NET 中的完成方式:

DateSerial(Year(DateAdd("m",
AmountOfMonths,
CDate(Invoice_date))),
Month(DateAdd(DateInterval.Month,
AmountOfMonths, CDate(Invoice_date))), 28)

到目前为止我尝试了什么:

var currentDate = new Date();
var day = currentDate.getDate() ;
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
if  (day <= "28") result += day ="28";
else result += day ="28";
if  (day > "28") result = month + "1";
results.html("<b>" + year + "/" + month + "/" + day + "</b>");

如果天是 29、30 或 31,我已经无法设置下个月。然后我需要通过添加月份(5、7 或 15 或任意数字)来计算新日期。

【问题讨论】:

    标签: javascript date dayofmonth


    【解决方案1】:

    您可以使用 toLocaleDateString() 和 javascript Date 对象获取器和设置器方法尝试此操作:

    // Returns a string in mm/dd/yyyy format
    const GetFormattedDate = d => d.toLocaleDateString('en-US');
    
    const CalculateDate = (dateStr) => {
      var currentDate = dateStr ? new Date(dateStr) : new Date();
    
      // Log the current actual date
      console.log('Actual date: ', GetFormattedDate(currentDate));
    
      var day = currentDate.getDate();
      if (day > 28) {
        currentDate.setMonth(currentDate.getMonth() + 1);
      }
      currentDate.setDate(28);
    
      // Return 28th of current or next month
      return GetFormattedDate(currentDate);
    }
    
    // 1 week ago
    console.log('Updated date: ', CalculateDate('02/21/2020'))
    
    // Today
    console.log('Updated date: ', CalculateDate())
    
    // 1 week later
    console.log('Updated date: ', CalculateDate('03/06/2020'))
    
    // 1 month later
    console.log('Updated date: ', CalculateDate('03/29/2020'))
    
    // 1 year later
    console.log('Updated date: ', CalculateDate('02/21/2021'))
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案2】:

      要添加月份,请在 Date 中的月份中添加一个。仅当日期大于 28 时才需要做某事,例如

      var currentDate = new Date();
      var currentDay = currentDate.getDate() ;
      currentDate.setDate(28);
      
      if  (currentDay > 28) {
        currentDate.setMonth(currentDate.getMonth() + 1);
      }
      
      var month = currentDate.getMonth() + 1;
      var year  = currentDate.getFullYear();
      
      console.log(year + '/' + month + '/' + 28);

      【讨论】:

      • 这对我来说很好:)var field = ""; field = record.fields["CreditTime2"]; var currentDate = new Date(); var currentDay = currentDate.getDate() ; currentDate.setDate(28); if (currentDay > 28) { currentDate.setMonth(currentDate.getMonth() + 1); } //将月数添加到已经计算的日期 currentDate.setMonth(currentDate.getMonth() + parseInt(field)); var 月 = currentDate.getMonth() + 1; var year = currentDate.getFullYear(); results.html(年 + '-' + 月 + '-' + 28);
      • @Greg——不要在 cmets 中发布代码,将它们放在答案中。如果其中一个答案回答了您的问题,请接受它(包括您自己的),以免其他人认为您仍在寻找答案。如果您想修改您的问题,请执行此操作。不要在你的答案中修改它。
      【解决方案3】:

      这是对我有用的代码!谢谢大家帮助我!

      var field = "";
      field = record.fields["CreditTime2"];
      var currentDate = new Date();
      var currentDay = currentDate.getDate() ;
      currentDate.setDate(28);
      if (currentDay > 28) { currentDate.setMonth(currentDate.getMonth() + 1);
      }
      //add number of months to already calculated date
      currentDate.setMonth(currentDate.getMonth() + parseInt(field));
      var month = currentDate.getMonth() + 1;
      var year = currentDate.getFullYear();
      results.html(year + '-' + month + '-' + 28); 
      

      【讨论】:

        猜你喜欢
        • 2021-10-26
        • 1970-01-01
        • 1970-01-01
        • 2021-09-29
        • 1970-01-01
        • 2013-05-28
        • 1970-01-01
        • 1970-01-01
        • 2013-09-17
        相关资源
        最近更新 更多