Skip to content

Instantly share code, notes, and snippets.

@mei23
Created April 13, 2019 16:44
Show Gist options
  • Save mei23/48e39952458726c1b7e71dd615c41cbf to your computer and use it in GitHub Desktop.
Save mei23/48e39952458726c1b7e71dd615c41cbf to your computer and use it in GitHub Desktop.
const CHARS = '0123456789abcdef';
function getTime(time: number) {
if (time < 0) time = 0;
if (time === 0) {
return CHARS[0];
}
time += 0x800000000000;
return time.toString(16).padStart(12, CHARS[0]);
}
function getRandom() {
let str = '';
for (let i = 0; i < 12; i++) {
str += CHARS[Math.floor(Math.random() * CHARS.length)];
}
return str;
}
export function genObjectId(date: Date): string {
return getTime(date.getTime()) + getRandom();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment