【问题标题】:Saving bought date and adding X time to it [duplicate]保存购买日期并为其添加 X 时间 [重复]
【发布时间】:2017-10-20 19:45:29
【问题描述】:

我正在创建一个节点应用程序,用户可以使用以下 HTML 输入选择 bought 日期:<input placeholder="Date Bought" class="textbox-n form-control" type="text" onfocus="(this.type='date')" onblur="(this.type='text')" id="bought" name="bought">

然后使用 var bought = req.body.bought; 将其存储到 mongodb 中,并且用户将几个月输入到另一个具有 name="warranty" 的 HTML 输入中,我需要使用 bought 日期和 warranty 月份转换为日期.我怎样才能添加这些?我试过这个:

  var bought = req.body.bought;
  var warrantyMonths = req.body.warranty;
  var warranty = bought.setMonth(bought.getMonth()+ warrantyMonths); 

据此guide

【问题讨论】:

    标签: javascript node.js date


    【解决方案1】:

    尝试使用Moment.js

    如果保修月份是整数,您可以:

    var bought = moment(req.body.bought);
    var warrantyMonths = req.body.warranty;
    var warranty = moment(bought).add(warrantyMonths, "month");
    

    Moment 只是 JS 日期对象的包装器,因此它无缝且易于使用。您可以将其安装为 npm 包,将其导入您的页面,并将其传递给不同格式的日期字符串(请参阅文档)或任何有效的 javascript 日期对象。我不能再无时间处理日期了,imo 它应该是标准库的一部分。

    【讨论】:

    • 我试过了,我使用 NPM 安装了 moment。然后我把你的代码粘贴进去并提交了我的表格,这是我的购买日期"bought" : ISODate("2018-05-01T00:00:00Z"),,然后这是我的保修日期"warranty" : ISODate("2018-05-01T00:00:00Z"),,我使用 12 个月作为保修日期。
    • 哎呀将 moment(bought.add(warrantyMonths, "month")) 更改为 moment(bought).add(warrantyMonths, "month") 那是我的错。第一行是改变购买的对象,但您要做的是首先通过调用 moment(bought) 克隆它,然后调用 add 方法
    • 非常感谢,我已将您的答案标记为正确。
    【解决方案2】:

    查看代码和 cmets 以了解您的代码无法正常工作的原因

    function calculateWarranty() {
      // get the date text and convert into date Object
      var bought = new Date(document.getElementById('bought').value);
      // get the number of warranty months and convert to integer (second arguments ensures that the integer is base 10)
      // if you do not convert the number, it will do String concatenation while calling dateObj.setMonth(getMonth_int + warrant_string )
      // for example: 2017-10-21 will give bought.getMonth() === 9 + "1", which will equal 91 instead of 10
      var warrantyMonths = parseInt(document.getElementById('warranty').value, 10);
    
      // display in console for debugging
      console.log(bought);
      console.log(warrantyMonths);
    
      var boughtMonth = bought.getMonth();
    
      // add bought month and warranty month (both should be integer as stated earlier)
      var warrantyMonth = boughtMonth + warrantyMonths;
    
      // set the month to bought object
      bought.setMonth(warrantyMonth);
    
      // result
      console.log(bought);
    }
    
    // adding event listeners on the input boxes
    document.querySelector('#bought').addEventListener('change', calculateWarranty);
    document.querySelector('#warranty').addEventListener('change', calculateWarranty);
    <input id="bought" name="bought" class="textbox-n form-control" type="text" placeholder="Date Bought" onfocus="(this.type='date')" onblur="(this.type='text')" />
    
    <input id="warranty" name="warranty" class="textbox-n form-control" type="number" value="1" />

    您还需要在代码中添加一些错误处理。

    【讨论】:

    • 感谢cmets,解释了很多。我确实喜欢 moment.js,因为它很简单,尽管我认为你的方法可以帮助我更多地了解 vanilla js
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2017-08-25
    • 1970-01-01
    相关资源
    最近更新 更多