Skip to content

Instantly share code, notes, and snippets.

@tracend
Created December 31, 2013 22:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tracend/8203090 to your computer and use it in GitHub Desktop.
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/
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;
}
@jonsch
Copy link

jonsch commented Jan 14, 2014

cool to see this implemented in js! nice work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment