I'm trying to remove last 3 zeroes here: 1437203995000
How do I do this in JavaScript? I'm generating the numbers from new date()
function.
To remove the last character from a string in JavaScript, you should use the slice() method. It takes two arguments: the start index and the end index. slice() supports negative indexing, which means that slice(0, -1) is equivalent to slice(0, str. length - 1) .
To remove the last n characters from a string, call the slice() method, passing it 0 and -n as parameters. For example, str. slice(0, -3) returns a copy of the original string with the last 3 characters removed.
To get the last N characters of a string, call the slice method on the string, passing in -n as a parameter, e.g. str. slice(-3) returns a new string containing the last 3 characters of the original string. Copied! const str = 'Hello World'; const last3 = str.
Here is an approach using str.slice(0, -n)
. Where n is the number of characters you want to truncate.
var str = 1437203995000; str = str.toString(); console.log("Original data: ",str); str = str.slice(0, -3); str = parseInt(str); console.log("After truncate: ",str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With