Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Gets time and date, generates a date based hash.

1. Gets time and date, generates a date based hash.

Browser independent date/time code. Leading zero are retained.

Code:
```
//  Gets date and time, generates a unique identifier based on date and time.

var timenow = new Date();

var YY = timenow.getYear();

var MM = timenow.getMonth() + 1;

var DD = timenow.getDate();

var HH = timenow.getHours();

var mm = timenow.getMinutes();

var ss = timenow.getSeconds();

if (YY <= 200){Y = YY + 1900}else{Y = YY};

if (MM <= 9){M = "0" + MM}else{M = MM};

if (DD <= 9){D = "0" + DD}else{D = DD};

if (HH <= 9){H = "0" + HH}else{H = HH};

if (mm <= 9){m = "0" + mm}else{m = mm};

if (ss <= 9){s = "0" + ss}else{s = ss};

var prehash = ((Y-1900) + M + D + H + m + s);

var datehash = (prehash-0).toString(36);

var fulldate = (Y +"_"+ M +"_"+ D);

var datetime = (Y +"_"+ M +"_"+ D +" @ "+ H +"_"+ m +"_"+ s);

2. Will be so kind and tell us what is the use of this code? Can you detail a little bit your great discovery?

3. Originally Posted by Kor
Will be so kind and tell us what is the use of this code? Can you detail a little bit your great discovery?
The use is whatever people have for it, or can adapt it to. It's quite flexible in that you can configure the results to your liking. You could use them to display on a web page or send information back to a server.

Sure, it's code that 'anybody' could have written, but that's true of all code, computer languages are completely artificial and of public knowledge. And please don't be too impressed with all the 'new' code, a lot of people in the world still use windows 98se with IE 6. Not where I live, maybe not where you live, but still millions of them.

4. Originally Posted by DrDOS
Sure, it's code that 'anybody' could have written, but that's true of all code,
Not only could anybody have written this, they already have - long ago.

Much better solution:-

Code:
```<script type = "text/javascript">

function UniqueString() {
var d = new Date().getTime();
var t = parseInt(d/1000);  // seconds
t = t.toString();
while (/^\d\d\d/.test(t)) {
t = t.replace(/^(\d*)(\d\d[\w\b])/, "\$1" + String.fromCharCode(64 + (Math.round(Math.random()*25)+1)) + "\$2");
}
t = t.substr(7,7); // take last 7 chars
t = t + String.fromCharCode(64 + (Math.round(Math.random()*25)+1));  // append a random letter
alert (t);     // an 8-character (virtually) unique alpha-numeric string
return t;
}

UniqueString();

< script>```

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•