1

I apologize if this question is too simple, I am just getting started in JavaScript. I have the following problem: A function is returning an array similar to:

[
  "Wed Feb 24 11:39:01 CST 2021",
  "Thu Mar 21 18:35:30 CDT 2019",
  "Tue Feb 26 15:23:50 CST 2019"
]

My challenge is to grab just the year from each of those elements within the array (so I need to grab 2021, 2019, and 2019).

I was thinking in my head to use .split by using space as the delimitator and then using length-1 to get the last element. But my implementation keeps throwing errors.

I tried:

var fromDate = $dateList[*].dates
var fromDateSplit = fromDate.split(' ');
var fromYear = fromDateSplit[fromDateSplit.length - 1]

Please let me know what I am doing wrong. Getting ready to pull hair out.

Thank you in advance for looking at this!

djm.im
  • 3,295
  • 4
  • 30
  • 45
hungryhippos
  • 617
  • 4
  • 15
  • 3
    First off, `Java != JavaScript`, and in fact they are very different. Please choose the one most appropriate language and delete the other tag. Looking to me like JavaScript is what you're asking about here, not Java. – Hovercraft Full Of Eels Jul 08 '21 at 18:57
  • What is `$dateList[*].dates` supposed to do? It looks like your dateList is just a string array. `*` isn't a valid index. – Charlie Bamford Jul 08 '21 at 19:00
  • `var x = [ "Wed Feb 24 11:39:01 CST 2021", "Thu Mar 21 18:35:30 CDT 2019", "Tue Feb 26 15:23:50 CST 2019" ]; const years = x.map(s => +s.match(/\d{4}$/)[0])` – epascarello Jul 08 '21 at 19:01
  • Does this answer your question? [How can I get last characters of a string](https://stackoverflow.com/questions/5873810/how-can-i-get-last-characters-of-a-string) – Heretic Monkey Jul 08 '21 at 19:07

4 Answers4

1

My challenge is to grab just the year from each of those elements within the array (so I need to grab 2021, 2019, and 2019).

With a simple loop and new Date(v).getFullYear().

const arr = [
  "Wed Feb 24 11:39:01 CST 2021",
  "Thu Mar 21 18:35:30 CDT 2019",
  "Tue Feb 26 15:23:50 CST 2019"
]

console.log(arr.map(v => new Date(v).getFullYear()))
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • There's no guarantee any JavaScript engine will parse a string like that as a date. See [Why does Date.parse give incorrect results?](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – Heretic Monkey Jul 08 '21 at 19:04
1

Simply split it by spaces and get the last item in the result array:

const a = [
  "Wed Feb 24 11:39:01 CST 2021",
  "Thu Mar 21 18:35:30 CDT 2019",
  "Tue Feb 26 15:23:50 CST 2019"
]
const b = a.map(e => e.split(" ").slice(-1)[0]);
console.log(b);
Spectric
  • 30,714
  • 6
  • 20
  • 43
1

Keep your hair on your head and follow me:

On second line, your variable 'fromDateSplit' is trying to split an array, not a string. The split method belongs to string objects. So, you can try iterate over your array and apply the split on the current element of the iteration. Like this:

var array = [
  "Wed Feb 24 11:39:01 CST 2021",
  "Thu Mar 21 18:35:30 CDT 2019",
  "Tue Feb 26 15:23:50 CST 2019"
]

array.forEach(element => {
    var fromDateSplit = element.split(' ');
    var fromYear = fromDateSplit[fromDateSplit.length - 1];
    console.log(fromYear);
});
Arthur
  • 21
  • 3
1

You could also look for the last space in the strings using string.lastIndexOf(" "), and take what comes afterwards.

const a = [
  "Wed Feb 24 11:39:01 CST 2021",
  "Thu Mar 21 18:35:30 CDT 2019",
  "Tue Feb 26 15:23:50 CST 2019"
]
const years = a.map((e) => e.substring(e.lastIndexOf(" ")));
console.log(years);
Mario Varchmin
  • 3,704
  • 4
  • 18
  • 33