Create a unique number with javascript time

后端 未结 30 2982
生来不讨喜
生来不讨喜 2020-12-02 08:31

I need to generate unique id numbers on the fly using javascript. In the past, I\'ve done this by creating a number using time. The number would be made up of the four digi

相关标签:
30条回答
  • 2020-12-02 08:44

    The shortest way to create a number that you can be pretty sure will be unique among as many separate instances as you can think of is

    Date.now() + Math.random()
    

    If there is a 1 millisecond difference in function call, it is 100% guaranteed to generate a different number. For function calls within the same millisecond you should only start to be worried if you are creating more than a few million numbers within this same millisecond, which is not very probable.

    For more on the probability of getting a repeated number within the same millisecond see https://stackoverflow.com/a/28220928/4617597

    0 讨论(0)
  • 2020-12-02 08:45

    In 2020, you can use the in-browser Crypto API to generate cryptographically strong random values.

    function getRandomNumbers() {
      const typedArray = new Uint8Array(10);
      const randomValues = window.crypto.getRandomValues(typedArray);
      return randomValues.join('');
    }
    
    console.log(getRandomNumbers());
    // 1857488137147725264738
    
    

    both Uint8Array and Crypto.getRandomValues are supported on all major browsers, including IE11

    0 讨论(0)
  • 2020-12-02 08:46
    let now = new Date();
    let timestamp = now.getFullYear().toString();
    let month = now.getMonth() + 1;
    timestamp += (month < 10 ? '0' : '') + month.toString();
    timestamp += (now.getDate() < 10 ? '0' : '') + now.getDate().toString();
    timestamp += (now.getHours() < 10 ? '0' : '') + now.getHours().toString();
    timestamp += (now.getMinutes() < 10 ? '0' : '') + now.getMinutes().toString();
    timestamp += (now.getSeconds() < 10 ? '0' : '') + now.getSeconds().toString();
    timestamp += (now.getMilliseconds() < 100 ? '0' : '') + now.getMilliseconds().toString();
    
    0 讨论(0)
  • 2020-12-02 08:47

    Easy and always get unique value :

    const uniqueValue = (new Date()).getTime() + Math.trunc(365 * Math.random());
    **OUTPUT LIKE THIS** : 1556782842762
    
    0 讨论(0)
  • 2020-12-02 08:47

    To get a unique number:

    function getUnique(){
        return new Date().getTime().toString() + window.crypto.getRandomValues(new Uint32Array(1))[0];
    }
    // or 
    function getUniqueNumber(){
        const now = new Date();
        return Number([
            now.getFullYear(),
            now.getMonth(),
            now.getDate(),
            now.getHours(),
            now.getMinutes(),
            now.getUTCMilliseconds(),
            window.crypto.getRandomValues(new Uint8Array(1))[0]
        ].join(""));
    }
    

    Example:

    getUnique()
    "15951973277543340653840"
    
    for (let i=0; i<5; i++){
        console.log( getUnique() );
    }
    15951974746301197061197
    15951974746301600673248
    15951974746302690320798
    15951974746313778184640
    1595197474631922766030
    
    getUniqueNumber()
    20206201121832230
    
    for (let i=0; i<5; i++){
        console.log( getUniqueNumber() );
    }
    2020620112149367
    2020620112149336
    20206201121493240
    20206201121493150
    20206201121494200
    

    you can change the length using:

    new Uint8Array(1)[0]
    // or
    new Uint16Array(1)[0]
    // or
    new Uint32Array(1)[0]
    
    0 讨论(0)
  • 2020-12-02 08:48

    Here's what I do when I want something smaller than a bunch of numbers - change base.

    var uid = (new Date().getTime()).toString(36)
    
    0 讨论(0)
提交回复
热议问题