DEV Community


Discussion on: Two string methods every JavaScript developer should know.

rubyrubenstahl profile image
Ruby Rubenstahl

If it's a fixed format you can just slice it out without having to spend CPU cycles searching.

const dt = '"2019-08-02T00:00:00.000Z"';
const date = dt.slice(1,11);

The methods in the article are definitely handy to have in your toolkit though.

brandito profile image
brandito • Edited

.slice(), .substr() and .substring() can all achieve this but I agree completely that it should be done with the fixed indexes since its quite apparent the data is all in the same format given his example and glad that my first thought was apparently the most efficient!

edit: turns out substr is non-standard and substring should be used instead