繁体   English   中英

将今天的日期分配给键并引用此键以添加 5 天

[英]Assigning Today's Date to a Key and referencing this Key to Add 5 days

我试图将今天的日期分配给contVfrmDate并使用值/日期将下一个键增加 x 天数? 可以在对象/字典中完成吗? 如果是,如何?

const ccCustInfo = {
  cropConsultant: 'Test this',
  customerName:   'Customer',
  customerBranch: 'MULBERRY ',
  shippingAddress:'address',
  contractType:   'SKU',
  contVfrom:      'Contract Valid From',
  contVto:        'Contract Valid To',
  internalNotes:  'Internal Notes',
  contVfrmDate:    (new Date(Date.now()).toLocaleString().split(',')[0]),
  contVtoDate:     contVfrmDate.setDate(contVfrmDate.getDate() + 5)
}

在 Chrome 控制台中,我看到了这个错误。

Uncaught ReferenceError: contVfrmDate is not defined
at <anonymous>:11:24

找到了另一个简单的解决方案,因为to date默认为 8 天。

contVtoDate: (new Date(Date.now() + (8 /*days*/ * 86400000 /*ms per day*/)).toLocaleString().split(',')[0])}

const ccCustInfo = {
          cropConsultant: 'Test this',
          customerName:   'Customer 5K FARMS',
          customerBranch: 'MULBERRY FL',
          shippingAddress:'3010',
          contractType:   'SKU',
          contVfrom:      'Contract Valid From',
          contVto:        'Contract Valid To',
          internalNotes:  'Internal Notes',
          contVfrmDate:    (new Date(Date.now()).toLocaleString().split(',')[0]),
          contVtoDate:     (new Date(Date.now() + (8 /*days*/ * 86400000 /*ms per day*/)).toLocaleString().split(',')[0])
}
       

您应该意识到使用.setDate()调整 Date 对象将更改原始日期。 所以你需要两个独立的日期对象:

    const frmDate = new Date();
    const toDate = new Date();

但是toDate需要添加 5 天。 最直接的方法是现在在toDate上使用.setDate()方法。 通过从frmDate获取日期并添加您的 5 天来frmDate

toDate.setDate(frmDate.getDate() + 5);

结果如下所示:

 const frmDate = new Date(); const toDate = new Date() toDate.setDate(frmDate.getDate() + 5); const ccCustInfo = { cropConsultant: 'Test this', customerName: 'Customer', customerBranch: 'MULBERRY ', shippingAddress:'address', contractType: 'SKU', contVfrom: 'Contract Valid From', contVto: 'Contract Valid To', internalNotes: 'Internal Notes', contVfrmDate: frmDate.toLocaleString('en-US',{month: 'numeric', day: 'numeric', year: 'numeric'}), contVtoDate: toDate.toLocaleString('en-US',{month: 'numeric', day: 'numeric', year: 'numeric'}) } console.log(ccCustInfo);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM