Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,036
    Thanks
    15
    Thanked 240 Times in 240 Posts

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

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Will be so kind and tell us what is the use of this code? Can you detail a little bit your great discovery?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,036
    Thanks
    15
    Thanked 240 Times in 240 Posts
    Quote Originally Posted by Kor View Post
    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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by DrDOS View Post
    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();
    //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>
    Last edited by Philip M; 09-27-2010 at 05:25 PM.


  •  

    Posting Permissions

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