Link to home
Start Free TrialLog in
Avatar of MichaelKThomas
MichaelKThomas

asked on

Unique ID in Javascript

I would like to create a unique ID using the date and time in javascript.  How do I do that?
Avatar of stefanaichholzer
stefanaichholzer
Flag of United Kingdom of Great Britain and Northern Ireland image

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...

;)
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>
ASKER CERTIFIED SOLUTION
Avatar of stefanaichholzer
stefanaichholzer
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<script>
alert(new Date().getTime());
</script>
ldbkutty,

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

 Thanx! ;)
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?
getTime() returns the unix timestamp in milli-seconds.
ldbkutty,

Just as I imagined. Thanx. ;)
Avatar of SnowFlake
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.
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!

;)
Avatar of dakyd
dakyd

>> 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.
> 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
Unique ID using date and time:
ID = new Date().getTime()
As shown in  https://www.experts-exchange.com/questions/21391346/Unique-ID-in-Javascript.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?
Avatar of MichaelKThomas

ASKER

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?
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...

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

;)
and you miss the <!--
and it is window.location rather than document.location
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!

;)
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
http://www.mozilla.org/docs/dom/domref/dom_doc_ref26.html

document.location is SUPPOSEDLY read only and deprecated.
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
Most browsers allow
document.location="..."
But it is not according to the specs
You are right again, I was only refering to the logic behind the specs.
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>

;)
Is there a way to get a shorter number?