Skip to content

Instantly share code, notes, and snippets.

@antonioaguilar
Created January 23, 2017 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save antonioaguilar/6135f84658328d399ed656ba3169e558 to your computer and use it in GitHub Desktop.
Save antonioaguilar/6135f84658328d399ed656ba3169e558 to your computer and use it in GitHub Desktop.
Generate UUID in Browser
function UUID() {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
}
@ChenLiZhan
Copy link

ChenLiZhan commented Mar 12, 2018

Happy to find out this function. I'm wondering why there is a 4 inside the uuid format?

@mattbishop
Copy link

@ChenLiZhan the4 is the version of the UUID, in this case v4.

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