Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to slice specifc part of a string JS?

I’m trying to slice a specific part of a string, but could not find any information on it.
I want to remove the first set of 00s and : from 10:00:00 AM without removing the AM so 10:00AM.

I could only find out how to slice from the back or front using -,+ symbols but not from the middle.

EDIT – Only want var ‘dt’ sliced not looking for a solution for other time values.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var dt = 10:00:00 AM
const result1 = dt.toLocaleString().slice(Number Here);
console.log(result1)
//want the result to be 10:00 AM

Thanks!

>Solution :

You can do the following:

const dt = '10:00:00 AM';

const result1 = dt.slice(0, 3) + dt.slice(6);

console.log(result1);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading