-4

I have a generate id button, when I click on the button it generates the date '2013-06-13' in a textbox. I want to add a 4 digit counter to the end of it so it will look like this '2013-06-13-xxxx' (0001, 0002, etc). When I click on the the generate button I want it to show '2013-06-13-0001' then I will push a submit button and the form will reset. When I push the generate id button again I want it to show '2013-06-13-0002' what code may I use to do this?

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

This is the output code

function guidGenerator() {
        var theID = (Year() + "-" + Month() + "-" + Day() + "-" + Counter);
        return theID;
    }
user2457203
  • 41
  • 2
  • 11
  • Um.. Isn't it the question I just answered for you? http://stackoverflow.com/questions/17090634/unique-id-that-gets-a-date-in-specific-format/17091326#17091326 – Yuriy Galanter Jun 13 '13 at 16:39
  • Yessir, it is. But it was a bit hard for me to understand that code. I ended up using code that I could understand but only up to the point where the date displays. I just need something to fill in for the counter. – user2457203 Jun 13 '13 at 16:43

2 Answers2

0

You can use this:

var str = '000' + Counter();
var result = str.substring(str.length - 4);
var theID = (Year() + "-" + Month() + "-" + Day() + "-" + result);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
0

I think here is what you looking for How to output integers with leading zeros in JavaScript

like this function below

function pad(num, size) {
    var s = "0000" + num;
    return s.substr(s.length-size);
}

and actually you just need s = "000" + num;

Community
  • 1
  • 1