唯一的ID获取特定格式的日期? [英] Unique ID that gets a date in specific format?

查看:86
本文介绍了唯一的ID获取特定格式的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code,它会产生一个随机唯一的ID ..但有什么办法,使其抓住像YYYY-MM-DD-0001以特定的方式的日期,我可以编辑这个code。最后4位数字,我希望它给每个generateid按钮被点击时加1。所以它会变成0002。这是当前code我有。有没有办法,可以自动抓取日期函数?

  VAR计数器= 0000;
    功能计数器(){
如果((的document.getElementById(generateid)。点击==真)
{
计数器++
返回柜台;
}
    }
    功能月份(){
        变种M =新的日期();
        变种毫米= m.getMonth()+ 1;
        如果(平方毫米; 10​​){
            毫米='0'+毫米;
            返回毫米;
        }
    }
    功能年(){
        变种Y =新的日期();
        变种YY = y.getFullYear();
        返回YY;
    }
    函数日(){
        变种D =新的日期();
        变种DD = d.getDate();
        返回DD;
    }
//生成ID
    功能guidGenerator(){
        VAR theID =(年()+ - +月()+ - +日()+ - +计数器);
        返回theID;
    }    功能generateID(){        变种TheTextBox =的document.getElementById(generateidtxt);
        TheTextBox.value = TheTextBox.value + guidGenerator();
        的document.getElementById(generateid)禁用= TRUE。    }


解决方案

您可以使用以下对象:

  VAR idGenerator = {
    SEQ:0,
    generateId:功能(){
       this.seq ++;
       回报(新的Date())toISOString()子(0,10)+' - '+(000 + this.seq).substr(-4)
    }
}

声明这样后,尝试

 函数generateID(){
   变种TheTextBox =的document.getElementById(generateidtxt);
   TheTextBox.value = TheTextBox.value + idGenerator.generateId();
   的document.getElementById(generateid)禁用= TRUE。
}

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 want it to add 1 each time the generateid button is clicked. so it will change to 0002. Here is the current code I have. Is there a function that can grab the date automatically?

  var counter = 0000;
    function Counter() {
if((document.getElementById("generateid").clicked == true)
{
Counter++
return counter;
}
    }




    function Month() {
        var m = new Date();
        var mm = m.getMonth() + 1;
        if (mm < 10) {
            mm = '0' + mm;
            return mm;
        } 


    }
    function Year() {
        var y = new Date();
        var yy = y.getFullYear();
        return yy;
    }
    function Day() {
        var d = new Date();
        var dd = d.getDate();
        return dd;
    }
//generate id
    function guidGenerator() {
        var theID = (Year() + "-" + Month() + "-" + Day() + "-" + Counter);
        return theID;
    }

    function generateID() {

        var TheTextBox = document.getElementById("generateidtxt");
        TheTextBox.value = TheTextBox.value + guidGenerator();
        document.getElementById("generateid").disabled = true;

    }

解决方案

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

这篇关于唯一的ID获取特定格式的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆