Created
December 31, 2013 22:56
-
-
Save tracend/8203090 to your computer and use it in GitHub Desktop.
Unique Code: Generate short IDs based on UTC
Based on the C# version: http://schroedman.wordpress.com/2012/01/19/short-unique-id-in-c-without-using-guid/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function uniqueCode(){ | |
var characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
var ticks = (new Date()).getTime().toString(); | |
var code = ""; | |
for (var i = 0; i < characters.length; i += 2) { | |
if ((i + 2) <= ticks.length) { | |
var number = parseInt(ticks.substr(i, 2)); | |
if (number > characters.length - 1) { | |
var one = number.toString().substr(0, 1); | |
var two = number.toString().substr(1, 1); | |
code += characters[parseInt(one)]; | |
code += characters[parseInt(two)]; | |
} else { | |
code += characters[number]; | |
} | |
} | |
} | |
return code; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool to see this implemented in js! nice work!