Create a unique number with javascript time

后端 未结 30 2980
生来不讨喜
生来不讨喜 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:53

    Always get unique Id in JS

    function getUniqueId(){
       return (new Date().getTime()).toString(36) + new Date().getUTCMilliseconds();
    }
    
    getUniqueId()    // Call the function
    
    ------------results like
    
    //"ka2high4264"
    
    //"ka2hj115905"
    
    //"ka2hj1my690"
    
    //"ka2hj23j287"
    
    //"ka2hj2jp869"
    
    0 讨论(0)
  • 2020-12-02 08:54
        function UniqueValue(d){
            var dat_e = new Date();
            var uniqu_e = ((Math.random() *1000) +"").slice(-4)
    
            dat_e = dat_e.toISOString().replace(/[^0-9]/g, "").replace(dat_e.getFullYear(),uniqu_e);
            if(d==dat_e)
                dat_e = UniqueValue(dat_e);
            return dat_e;
        }
    

    Call 1: UniqueValue('0')
    Call 2: UniqueValue(UniqueValue('0')) // will be complex

    Sample Output:
    for(var i =0;i<10;i++){ console.log(UniqueValue(UniqueValue('0')));}
    60950116113248802
    26780116113248803
    53920116113248803
    35840116113248803
    47430116113248803
    41680116113248803
    42980116113248804
    34750116113248804
    20950116113248804
    03730116113248804

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

    From investigating online I came up with the following object that creates a unique id per session:

            window.mwUnique ={
            prevTimeId : 0,
            prevUniqueId : 0,
            getUniqueID : function(){
                try {
                    var d=new Date();
                    var newUniqueId = d.getTime();
                    if (newUniqueId == mwUnique.prevTimeId)
                        mwUnique.prevUniqueId = mwUnique.prevUniqueId + 1;
                    else {
                        mwUnique.prevTimeId = newUniqueId;
                        mwUnique.prevUniqueId = 0;
                    }
                    newUniqueId = newUniqueId + '' + mwUnique.prevUniqueId;
                    return newUniqueId;                     
                }
                catch(e) {
                    mwTool.logError('mwUnique.getUniqueID error:' + e.message + '.');
                }
            }            
        }
    

    It maybe helpful to some people.

    Cheers

    Andrew

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

    This performs faster than creating a Date instance, uses less code and will always produce a unique number (locally):

    function uniqueNumber() {
        var date = Date.now();
    
        // If created at same millisecond as previous
        if (date <= uniqueNumber.previous) {
            date = ++uniqueNumber.previous;
        } else {
            uniqueNumber.previous = date;
        }
    
        return date;
    }
    
    uniqueNumber.previous = 0;
    

    jsfiddle: http://jsfiddle.net/j8aLocan/

    I've released this on Bower and npm: https://github.com/stevenvachon/unique-number

    You could also use something more elaborate such as cuid, puid or shortid to generate a non-number.

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

    if you want a unique number after few mili seconds then use Date.now(), if you want to use it inside a for loop then use Date.now() and Math.random() together

    unique number inside a for loop

    function getUniqueID(){
        for(var i = 0; i< 5; i++)
          console.log(Date.now() + ( (Math.random()*100000).toFixed()))
    }
    getUniqueID()
    

    output:: all numbers are unique

    15598251485988384 155982514859810330 155982514859860737 155982514859882244 155982514859883316

    unique number without Math.random()

    function getUniqueID(){
            for(var i = 0; i< 5; i++)
              console.log(Date.now())
        }
        getUniqueID()
    

    output:: Numbers are repeated

    1559825328327 1559825328327 1559825328327 1559825328328 1559825328328

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

    use this:for creating unique number in javascript

    var uniqueNumber=(new Date().getTime()).toString(36);
    

    It really works. :)

    0 讨论(0)
提交回复
热议问题