Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

google apps script - parse array of dates string in javascript

I have array of datestring that consist of date and time and I would like to split the date time as there is '@' in between and combine them together in an array. I have developed the below script and find that I am getting only the last date value.

dates = ['1-12-21 @ 12:31 AM', '1-26-21 @ 09:45 AM', '12-14-20 @ 2:22 PM']
for(var i =0; i<dates.length;i++) {
  var array = new Array();
  array = dates[i].split('@');
  var newDate = new Date(array[0] + " " + array[1]);
  return newDate;}

I am getting the final value of 12-14-20 2:22 PM and not an array of dates. Instead I would like to get the array of date strings to date values.

I am new in javascript and so slowly catching up things.

question from:https://stackoverflow.com/questions/66049081/parse-array-of-dates-string-in-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

by having a return statemet, I assume this code is inside a function

if you put the return statement inside the for, it will exit returning the newDate value.

put the return outside the for statement, and push the dates in an array

function getDates()
{
  dates = ['1-12-21 @ 12:31 AM', '1-26-21 @ 09:45 AM', '12-14-20 @ 2:22 PM']
  var datesArray= new Array();
  for(var i =0; i<dates.length;i++) {
     var array = new Array();
     array = dates[i].split('@');
     var newDate = new Date(array[0] + " " + array[1]);
     datesArray.push(newDate)
  
  }
  return datesArray;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...