PDA

View Full Version : Gets time and date, generates a date based hash.


DrDOS
09-16-2010, 04:34 AM
Browser independent date/time code. Leading zero are retained.




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

alert(prehash);

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

alert(datehash);

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

alert(fulldate);

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

alert(datetime);

Kor
09-22-2010, 04:24 PM
Will be so kind and tell us what is the use of this code? Can you detail a little bit your great discovery?

DrDOS
09-22-2010, 06:21 PM
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.

Philip M
09-27-2010, 05:23 PM
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. :p

Much better solution:-

<script type = "text/javascript">

function UniqueString() {
var d = new Date().getTime();
var t = parseInt(d/1000); // seconds
t = t.toString();
//alert (t)
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");
}
//alert (t);
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>