Remove last 3 characters of string or number in javascript

前端 未结 3 653
孤街浪徒
孤街浪徒 2020-12-23 23:52

I\'m trying to remove last 3 zeroes 1437203995000

How do i this in javascript. Im generating the numbers from new date() function

相关标签:
3条回答
  • 2020-12-24 00:30

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

    0 讨论(0)
  • 2020-12-24 00:32

    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
    
    0 讨论(0)
  • 2020-12-24 00:52

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

    var a = 1437203995000;
    a = (a)/1000;
    
    0 讨论(0)
提交回复
热议问题