【问题标题】:How can I add and delete Date objects in arrays?如何在数组中添加和删除 Date 对象?
【发布时间】:2019-03-27 23:53:45
【问题描述】:

我正在尝试通过将组件 Datepicker 与 Vue.js 一起使用来更新日历,但是在添加和删除项目(在本例中为 Date 对象)方面存在一些问题

我已经开发了两个 javascript 函数:一个用于将新日期添加到数组中,另一个用于删除同一数组中的特定日期。问题是,当网站加载时,我可以完美地添加新日期并删除最初创建的日期。但是我不能做的是添加一个新的日期然后删除它,因为我开发的函数,用于返回数组中日期索引的方法返回-1。

addDate: function(event) {
   var fecha = document.getElementById("inputFecha").value;
   var fecha2 = new Date(fecha);
   availableDates.push(fecha2);
},
deleteDate: function(event) {
   var collection = availableDates,
      d = new Date(event.getFullYear(), event.getMonth(), event.getDate()),
      idx;
   idx = collection.map(Number).indexOf(+d);
   if(idx!=-1){
      availableDates.splice(idx,1);
   }
}

以及我最初在同一个文件中创建的一些日期:

var availableDates = [];
availableDates.push(new Date(2019, 2, 29));
availableDates.push(new Date(2019, 2, 30));
availableDates.push(new Date(2019, 2, 28));

我需要的是无需为网站充值即可添加和删除的可能性。

【问题讨论】:

  • 如果你能创建一个minimal reproducible example 会很有帮助,这样我们就可以运行代码并重现问题。你可以使用Stack Snippets
  • 请展示fecha的典型内容
  • 也许可以试试availableDates.push(new Date(fecha2.getFullYear(), fecha2.getMonth(), fecha2.getDate()))
  • 没关系 - 但@StefanBlamberg - 如果你在格林威治标准时间以西,这可以解决

标签: javascript arrays date vue.js


【解决方案1】:

当调用new Date(2019, 2, 30) 时,创建的日期是2019-03-30T00:00:00 在您的时区 ...

new Date("30/3/2019") 将创建 2019-03-30T00:00:00Z - 即 UCT/UTC/GMT/Zulu,无论您如何称呼它 - 如果您不在 +0 时区,那么这将失败 不确定是什么我在想这里......完全和彻底的垃圾:p

如果 inputFecha 是 <input type="date">

addDate: function(event) {
    var fecha = document.getElementById("inputFecha").valueAsDate;
    var fecha2 =new Date(fecha.getFullYear(), fecha.getMonth(), fecha.getDate());
   availableDates.push(fecha2);
},

由于您未能添加 MCVE,因此以下代码是一个粗略的假人,以表明它确实有效

const availableDates = [];
let something = {
  addDate: function(event) {
    var fecha = document.getElementById("inputFecha").valueAsDate;
    var fecha2 = new Date(fecha.getFullYear(), fecha.getMonth(), fecha.getDate());
    availableDates.push(fecha2);
    console.log(availableDates);
  },
  deleteDate: function(event) {
    var collection = availableDates,
      d // = new Date(event.getFullYear(), event.getMonth(), event.getDate()),
    
    // dummy code to test
    var fecha = document.getElementById("inputFecha").valueAsDate;
    d = new Date(fecha.getFullYear(), fecha.getMonth(), fecha.getDate());
    // end dummy code
    
    const idx = collection.map(Number).indexOf(+d);
    if (idx != -1) {
      availableDates.splice(idx, 1);
    }
    console.log(availableDates);
  }
}
availableDates.push(new Date(2019, 2, 29));
availableDates.push(new Date(2019, 2, 30));
availableDates.push(new Date(2019, 2, 28));
console.log(availableDates);
document.querySelectorAll('.butt').forEach(btn =>
  btn.addEventListener('click', something[btn.id])
);
<input type="date" id="inputFecha">
<input type="button" class="butt" id="addDate" value="add">
<input type="button" class="butt" id="deleteDate" value="remove">

【讨论】:

  • 谢谢!它完全适合我。但是,我使用的是 text 类型的输入,同时 .valueAsDateonly 方法适用于 date 类型的输入。我所做的是:var fecha = document.getElementById("inputFecha").value; var fecha2 =new Date(fecha); var fecha3 = new Date(fecha2.getFullYear(), fecha2.getMonth(), fecha2.getDate()); availableDates.push(fecha3); 它奏效了!
  • 这不一定行得通,我认为这取决于您的时区 - 所以如果您想要全球一致的东西,您可能会遇到问题 - 老实说,在这种情况下使用类似 momentjs 库的东西是可能是要走的路 - 你可以获得更好的一致性
猜你喜欢
  • 2021-06-30
  • 2017-07-19
  • 1970-01-01
  • 2020-05-23
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
  • 2021-11-23
相关资源
最近更新 更多