Solved

unique number generator

Posted on 2006-06-19
11
634 Views
Last Modified: 2012-06-27
How do you create a javascript number generator using the year,month,day, hour, and minute set on my computer?


And if possible, how do i format this number to be 10 characters in length with the format of YYMMDDHHmm?

Y=year,M=month,D=day,H=hour,m=minute
0
Comment
Question by:lvandevender
  • 6
  • 4
11 Comments
 
LVL 28

Accepted Solution

by:
Pravin Asar earned 300 total points
ID: 16936180
<script language="javascript">
function make2digit(value) {
     if (parseInt(value) < 10) { return ('0'+value); }
     return (value);
}
function GenerateUnique() {
   var dt = new Date();
   var str = '';
   str      += (dt.getYear()-2000);
   str      += make2digit(dt.getMonth()+1);
   str      += make2digit(dt.getDay());
   str      += make2digit(getHours());
   str      += make2digit(getMinutes());
}
alert (GenerateUnique());
</script>
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16936190
Please take this

<html>
<body>
<script language="javascript">
function make2digit(value) {
     if (parseInt(value) < 10) { return ('0'+value); }
     return (value);
}
function GenerateUnique() {
   var dt = new Date();
   var str = '';
   str      += (dt.getYear()-2000);
   str      += make2digit(dt.getMonth()+1);
   str      += make2digit(dt.getDay());
   str      += make2digit(dt.getHours());
   str      += make2digit(dt.getMinutes());
   return str;
}
alert (GenerateUnique());
</script>
</body>
</html>
0
 
LVL 11

Expert Comment

by:raj3060
ID: 16936547
slight modification to pravin's code:

str      += make2digit(dt.getDate());
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16937137
Thanks raj3060 for pointing that out. Although i had corrected before your post.

To display the number on page

<html>
<body>
<script language="javascript">
function make2digit(value) {
     if (parseInt(value) < 10) { return ('0'+value); }
     return (value);
}
function GenerateUnique() {
   var dt = new Date();
   var str = '';
   str      += (dt.getYear()-2000);
   str      += make2digit(dt.getMonth()+1);
   str      += make2digit(dt.getDay());
   str      += make2digit(dt.getHours());
   str      += make2digit(dt.getMinutes());
   return str;
}
alert (GenerateUnique());
document.form1.unqfld.value = GenerateUnique();
document.getElementById('unqdiv').innerHTML = GenerateUnique();
</script>
</body>
<form name="form1">
<input type="text" name="unqfld" readonly value="">
</form>
<div id="unqdiv">UNIQUE NUMBER GOES HERE</div>
</html>


0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16937159
lvandevender,

you could have closed this one before other one.

Anyway, close this one now.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 63

Expert Comment

by:Zvonko
ID: 16938207
How about this:

<html>
<head>
<title>Zvonko &#42;</title>
<script>
function getUnique(theBtn){
  var theForm = theBtn.form;
  theForm.BPnr.value = (new Date()).getTime().toString().substr(3);
}
</script>
</head>
<body>
<form>
<input type="button" value="Generate" onClick="getUnique(this)">
<br>
<input type="text" name="BPnr" size="10" maxlength="10">
</form>
</body>
</html>

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16938214
Or if the first digit should not be zero:

<html>
<head>
<title>Zvonko &#42;</title>
<script>
function getUnique(theBtn){
  var theForm = theBtn.form;
  theForm.BPnr.value = "7"+(new Date()).getTime().toString().substr(4);
}
</script>
</head>
<body>
<form>
<input type="button" value="Generate" onClick="getUnique(this)">
<br>
<input type="text" name="BPnr" size="10" maxlength="10">
</form>
</body>
</html>

0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16943069
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16945885
Stealing? Does my code version has to do ANYTHING with yours?
Or do you want this pointz ;-)
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16984252
Guys,


This is not about the points.

Visit the pages,

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21892486.html

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21891575.html

Asker seeks help based on the my post for this.

I have not problem with that.

For some reason , asker wants to close the Question, and get points refunded
that is okay by me.


_PA
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16984402
And what from YOUR code you think is stolen by me?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now