Unique ID in Javascript

I would like to create a unique ID using the date and time in javascript.  How do I do that?
MichaelKThomasAsked:
Who is Participating?
 
stefanaichholzerConnect With a Mentor Commented:
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
 
stefanaichholzerCommented:
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
 
stefanaichholzerCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
ldbkuttyCommented:
<script>
alert(new Date().getTime());
</script>
0
 
stefanaichholzerCommented:
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
 
stefanaichholzerCommented:
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
 
ldbkuttyCommented:
getTime() returns the unix timestamp in milli-seconds.
0
 
stefanaichholzerCommented:
ldbkutty,

Just as I imagined. Thanx. ;)
0
 
SnowFlakeCommented:
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
 
stefanaichholzerCommented:
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
 
dakydCommented:
>> 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
 
SnowFlakeCommented:
> 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
 
Michel PlungjanIT ExpertCommented:
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
 
MichaelKThomasAuthor Commented:
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
 
stefanaichholzerCommented:
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
 
stefanaichholzerCommented:
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
 
Michel PlungjanIT ExpertCommented:
and you miss the <!--
and it is window.location rather than document.location
0
 
stefanaichholzerCommented:
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
 
SnowFlakeCommented:
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
 
Michel PlungjanIT ExpertCommented:
http://www.mozilla.org/docs/dom/domref/dom_doc_ref26.html

document.location is SUPPOSEDLY read only and deprecated.
0
 
SnowFlakeCommented:
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
 
Michel PlungjanIT ExpertCommented:
Most browsers allow
document.location="..."
But it is not according to the specs
0
 
SnowFlakeCommented:
You are right again, I was only refering to the logic behind the specs.
0
 
stefanaichholzerCommented:
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
 
MichaelKThomasAuthor Commented:
Is there a way to get a shorter number?
0
 
Michel PlungjanIT ExpertCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.