如何无时间将新日期推送到数组?

问题描述 投票:-2回答:1

尝试以下操作,但我也得到了分钟,而我只需要按日/月/年的顺序(按此顺序)

      for (s = 0; s<dataSetCopy.length; s++) {
        newdataSetCopy.push(new Date(dataSetCopy[s]));
      }

      console.log(newdataSetCopy);

dataSetCopy数组具有类似于3/02/20的日期,我需要将其转换为3/02/2020

我正在尝试的完整代码:

      let dataSetCopy=[];
      let newdataSetCopy=[];
      initialDataSet.forEach(ds=>{
        ds.data.forEach( obj=>{
          dataSetCopy.push(obj.date)
        })
      })
      dataSetCopy.sort((a,b)=> Date.parse(a)-Date.parse(b));

      for (s = 0; s<dataSetCopy.length; s++) {
        newdataSetCopy.push(new Date(dataSetCopy[s]));
      }

      console.log(newdataSetCopy);
javascript
1个回答
0
投票

尝试一下,

for (s = 0; s<dataSetCopy.length; s++) {
    let current_datetime = new Date(dataSetCopy[s]);
    let formattedDate = current_datetime.getDate() + "/" + (current_datetime.getMonth() + 1) + "/" + current_datetime.getFullYear();
    newdataSetCopy.push(formattedDate);
}

console.log(newdataSetCopy);

© www.soinside.com 2019 - 2024. All rights reserved.