[JavaScript] 纯文本查看 复制代码 function 填充(str, targetLength, padString) {
str = String(str);
while (str.length < targetLength) {
str = padString + str;
}
return str;
}
function 时间() {
const now = new Date();
const year = now.getFullYear();
const month = 填充(now.getMonth() + 1, 2, '0');
const day = 填充(now.getDate(), 2, '0');
const hours = 填充(now.getHours(), 2, '0');
const minutes = 填充(now.getMinutes(), 2, '0');
const seconds = 填充(now.getSeconds(), 2, '0');
const milliseconds = 填充(now.getMilliseconds(), 3, '0');
const timestamp = `${year}${month}${day}${hours}${minutes}${seconds}${milliseconds}`;
return timestamp;
}
function generateRandomHex(length) {
let hex = '';
for (let i = 0; i < length; i++) {
hex += Math.floor(Math.random() * 16).toString(16);
}
return hex;
}
function formatAsUUID() {
const part1 = generateRandomHex(8);
const part2 = generateRandomHex(4);
const part3 = `8${generateRandomHex(3)}`;
const part4 = generateRandomHex(4);
const part5 = generateRandomHex(12);
return `${part1}-${part2}-${part3}-${part4}-${part5}`;
}
// 示例使用
const generatedUUID = formatAsUUID();
补充内容 (2024-10-24 17:35):
取当前时间 为 uuid 执行结果: 8cac2af6-859a-86b3-a94a-aad73b18e829 列如 20241024173523102
|