I have a string for example '12:13:45.123 UTC Sun Oct 17 2021' and I am looking to change the string into 'Sun Oct 17 2021 12:13:45.123 UTC'.
I do it with
str.slice(18)+' '+str.slice(0,17)
.
but the question is - how can I avoid call slice twice? is there a way to make it more elegant way and efficient?
BTW - I am not looking to split and concat the substrings.
You can use replace with a regex:
const str = '12:13:45.123 UTC Sun Oct 17 2021';
console.log(str.replace(/(.{16}) (.*)/, '$2 $1'));
IMHO it's more elegant but according to https://jsbench.me/ your code is faster.