teddybear123
teddybear123

Reputation: 2424

Remove last 3 characters of string or number in 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.

Upvotes: 130

Views: 212447

Answers (4)

Hemant Patel
Hemant Patel

Reputation: 51

var a = 1437203995123;
var b = (a / 1000) | 0; // 1437203995

// Note: Operator | ( Bitwise Or ) with ZERO converts value to integer 
// by discarding any value after decimal point 
// 0.9 | 0  ==> 0
// 0.1 | 0  ==> 0
// "string" | 0 ==> 0

Upvotes: 0

Usman Iqbal
Usman Iqbal

Reputation: 61

you just need to divide the Date Time stamp by 1000 like:

var a = 1437203995000;
a = (a)/1000;

Upvotes: 6

Hari Das
Hari Das

Reputation: 10849

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

Upvotes: 207

TaoPR
TaoPR

Reputation: 6052

Remove last 3 characters of a string

var str = '1437203995000';
str = str.substring(0, str.length-3);
// '1437203995'

Remove last 3 digits of a number

var a = 1437203995000;
a = (a-(a%1000))/1000;
// a = 1437203995

Upvotes: 69

Related Questions