?
Solved

Unique ID in Javascript

Posted on 2005-04-16
26
Medium Priority
?
10,836 Views
Last Modified: 2012-05-05
I would like to create a unique ID using the date and time in javascript.  How do I do that?
0
Comment
Question by:MichaelKThomas
  • 11
  • 5
  • 5
  • +3
26 Comments
 
LVL 9

Expert Comment

by:stefanaichholzer
ID: 13798035
MichaelKThomas,

This gives you the date:

<script type="text/javascript">
var d = new Date()
document.write(d.getDate()+d.getMonth() + 1+d.getFullYear()+d.getHours()+d.getMinutes()+d.getSeconds())
</script>

It will print the date and time in one line. You can assign that to a value.

Let me know how it goes...

;)
0
 
LVL 9

Expert Comment

by:stefanaichholzer
ID: 13798051
Bug, sorry, it's gotta be done like this:

<script type="text/javascript">
var d = new Date()
document.write(d.getDate()+""+d.getMonth() + 1+""+d.getFullYear()+""+d.getHours()+""+d.getMinutes()+""+d.getSeconds())
</script>
0
 
LVL 9

Accepted Solution

by:
stefanaichholzer earned 500 total points
ID: 13798057
MichaelKThomas,

And here assigned to a value:

<script type="text/javascript">
var d = new Date()
var ID = d.getDate()+""+d.getMonth() + 1+""+d.getFullYear()+""+d.getHours()+""+d.getMinutes()+""+d.getSeconds();
document.write(ID);
</script>

I guess you could have figured that out, don't you?

;)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 32

Expert Comment

by:ldbkutty
ID: 13798100
<script>
alert(new Date().getTime());
</script>
0
 
LVL 9

Expert Comment

by:stefanaichholzer
ID: 13798153
ldbkutty,

 I'm a bit confused now. What your script displays is different than mine, why?, would you mind explaining it to me?

 Thanx! ;)
0
 
LVL 9

Expert Comment

by:stefanaichholzer
ID: 13798171
ldbkutty,

I guess I know it. Mine is all the things concatenated, month, day, year and so on, your's show something like a unix timestamp, right?
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13798174
getTime() returns the unix timestamp in milli-seconds.
0
 
LVL 9

Expert Comment

by:stefanaichholzer
ID: 13798188
ldbkutty,

Just as I imagined. Thanx. ;)
0
 
LVL 15

Expert Comment

by:SnowFlake
ID: 13799217
b.t.w. stefanaichholzer,
even with your solution it would be enough to do

<script type="text/javascript">
var d = new Date()
var ID = '' + d.getDate()+d.getMonth() +1+d.getFullYear()+d.getHours()+d.getMinutes()+d.getSeconds();
document.write(ID);
</script>

when you start the +ing with a string the result is always a string
so a single empty string at the begining should do it.
I also dont know why you added the extra 1

SnowFlake.
0
 
LVL 9

Expert Comment

by:stefanaichholzer
ID: 13799561
SnowFlake,

Thank you for the update, didn't know that part: >> when you start the +ing with a string the result is always a string

The 1 slipped in by mistake!

;)
0
 
LVL 19

Expert Comment

by:dakyd
ID: 13799782
>> I also dont know why you added the extra 1

No, the 1 did not slip in by mistake.  The Date.getMonth() method will return a 0-based index for the given month.  In other words, getMonth() returns 0 for Jan, 1 for Feb, 2 for Mar, etc.  That's why you need to add the 1, so that you get 1 for Jan, 2 for Feb, ... In this case, parentheses would help to make sure you output the correct string.

That said, though, I think ldbkutty's solution is the cleanest.  It gives you a unique ID without having to parse out months, days, or years.  It's just simpler, even if it might look funny at first.  But that's just my two cents.
0
 
LVL 15

Expert Comment

by:SnowFlake
ID: 13800456
> That's why you need to add the 1

better expressed as "thats why if you want to have 1 for Jan you have to add 1"
and anyway in the way that it was done
it would have just appended the character 1 right next to the 0 based month
which would still make some sence for 1 for Jan (01) but would be wired for Feb (11) and
extra wierd for OCT (91) or  Dec (111) :)

other then that I agree that ldbkutty's solution is the cleanest,
depending on the amount of traffic and uniqueness importance level,
you might want to concatenate a random number
in addition to the date.

SnowFlake
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13800698
Unique ID using date and time:
ID = new Date().getTime()
As shown in  http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21391346.html#13798100

Points to kutty
PS I think Stefan wants +(d.getMonth() + 1)+

Sorry for adding to the noise. - I think this whole question should be cleaned up no?
0
 

Author Comment

by:MichaelKThomas
ID: 13808897
I am creating a form using Dreamweaver that generates a unique id number from the script below:

<script type="text/javascript">
var d = new Date()
var ID = d.getDate()+""+d.getMonth() + 1+""+d.getFullYear()+""+d.getHours()+""+d.getMinutes()+""+d.getSeconds();
document.write(ID);
</script>


It works great, however, I need to add a line of code into the asp page that will capture the number generated and send it to the email recipient.
How do I link the script above to my asp page?
0
 
LVL 9

Expert Comment

by:stefanaichholzer
ID: 13809248
MichaelKThomas,

I'm not much of an ASP guy, but in most cases it will be done like this:

<script type="text/javascript">
var d = new Date()
var ID = d.getDate()+""+d.getMonth() + 1+""+d.getFullYear()+""+d.getHours()+""+d.getMinutes()+""+d.getSeconds();

function redirect() {
document.location = "yourpage.asp?value="+ID
//-->

That will call your .sap page and pass it the JS (ID) value.

Let me know how it goes...

;)
0
 
LVL 9

Expert Comment

by:stefanaichholzer
ID: 13809280
Sorry, typo in the previous post!

<script type="text/javascript">
var d = new Date()
var ID = d.getDate()+""+d.getMonth() + 1+""+d.getFullYear()+""+d.getHours()+""+d.getMinutes()+""+d.getSeconds();

document.location = "yourpage.asp?value="+ID
//-->


Or this way for the email:

<script>
var d = new Date()
var ID = d.getDate()+""+d.getMonth() + 1+""+d.getFullYear()+""+d.getHours()+""+d.getMinutes()+""+d.getSeconds();
document.write("<a href=mailto:mail@domain.com?Body="+ID+">Mail me</a>");
</script>

;)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13809968
and you miss the <!--
and it is window.location rather than document.location
0
 
LVL 9

Expert Comment

by:stefanaichholzer
ID: 13809989
mplungjan,

Right, thank you for that. Tell me, please, why would you use window.location rather than document.location? I'm not too clear on that!

;)
0
 
LVL 15

Expert Comment

by:SnowFlake
ID: 13810122
MichaelKThomas,
what do you have in your ASP page ?
how does it expect to get the ID ?

the ID could be either sent as described above by stefanaichholzer

or it can be placed inside a hidden for field and sent via POST

what do you expect to happen in the browser window the user is watching ?
(Is this mail sent as a reaction to some user action ?)

why do you create the ID on the JS client side and not in the ASP to begin with ?

maybe you can give a little bit more details of what you are trying to do ...

SnowFalke
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13810161
http://www.mozilla.org/docs/dom/domref/dom_doc_ref26.html

document.location is SUPPOSEDLY read only and deprecated.
0
 
LVL 15

Expert Comment

by:SnowFlake
ID: 13810230
which makes a lot of sense if you think of document.location as the location from which the current document was loaded and window.location as the location the window is looking at.
you can not change the location of a specific document, but you can ask the window to view a document from another location.

SnowFlake
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13810318
Most browsers allow
document.location="..."
But it is not according to the specs
0
 
LVL 15

Expert Comment

by:SnowFlake
ID: 13810460
You are right again, I was only refering to the logic behind the specs.
0
 
LVL 9

Expert Comment

by:stefanaichholzer
ID: 13812153
Nothing missing, nothing too much:

<script type="text/javascript">
<!--
var d = new Date()
var ID = d.getDate()+""+d.getMonth() + 1+""+d.getFullYear()+""+d.getHours()+""+d.getMinutes()+""+d.getSeconds();
document.location = "yourpage.asp?value="+ID
//-->
</script>

Or this way for the email:

<script>
<!--
var d = new Date()
var ID = d.getDate()+""+d.getMonth() + 1+""+d.getFullYear()+""+d.getHours()+""+d.getMinutes()+""+d.getSeconds();
document.write("<a href=mailto:mail@domain.com?Body="+ID+">Mail me</a>");
//-->
</script>

;)
0
 

Author Comment

by:MichaelKThomas
ID: 13825812
Is there a way to get a shorter number?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13827524
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

749 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