Unique ID that gets a date in specific format?

后端 未结 2 530
滥情空心
滥情空心 2021-01-26 02:33

I have code that will generate a random unique id.. but is there a way I can edit this code so that it grabs a date in a specific way like yyyy-mm-dd-0001. the last 4 digits I w

2条回答
  •  [愿得一人]
    2021-01-26 03:21

    You can use the following object:

    var idGenerator  = {
        seq: 0,
        generateId: function () {
           this.seq++;
           return (new Date()).toISOString().substring(0, 10) + '-' + ('000' + this.seq).substr(-4)
        }
    }
    

    after declaration like this, try

    function generateID() {
       var TheTextBox = document.getElementById("generateidtxt");
       TheTextBox.value = TheTextBox.value + idGenerator.generateId();
       document.getElementById("generateid").disabled=true;    
    }
    

提交回复
热议问题