Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove last 3 characters of string or number in javascript

Tags:

javascript

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.

like image 729
teddybear123 Avatar asked Jul 18 '15 08:07

teddybear123


People also ask

How do I cut the last character of a string in JavaScript?

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) .

How do I remove the last n characters from a string?

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.

How do I get the last 5 characters of a string?

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.


1 Answers

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);
like image 177
Hari Das Avatar answered Oct 01 '22 10:10

Hari Das