Home > Blockchain >  How to slice specifc part of a string JS?
How to slice specifc part of a string JS?

Time:04-03

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.

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!

CodePudding user response:

You can do the following:

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

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

console.log(result1);

CodePudding user response:

You can try this one

var dt = '10:00:00 AM'
const result1 = dt.toLocaleString().split(" ")[0].slice(0, -3)  " "  dt.toLocaleString().split(" ")[1];
console.log(result1)
  • Related