Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using split pop and join all at once can it work?

Tags:

javascript

I really love Javascript and I wrote my code like this. I feel like it should work. Am I doing it in the wrong order? If it won't work like this why not?

var mydate = new Date();
alert( mydate.toLocaleTimeString().split(":").pop().join(':'));

split() makes it an array, pop() takes off the end of the array, join() makes it a string again right?

like image 822
Peter S McIntyre Avatar asked Oct 25 '25 19:10

Peter S McIntyre


1 Answers

You could use Array#slice with a negative end/second argument.

Array#pop returns the last element, but not the array itself. slice returns a copy of the array with all emements from start without the last element.

var mydate = new Date();
console.log(mydate.toLocaleTimeString().split(":").slice(0, -1).join(':'));
like image 54
Nina Scholz Avatar answered Oct 27 '25 10:10

Nina Scholz