首页 技术 正文
技术 2022年11月14日
0 收藏 424 点赞 4,965 浏览 1624 个字

function GUID() {

this.date = new Date();  //先new一个时间备用

GUID.prototype.newGUID = function () {
this.date = new Date();
var guidStr = ”;
sexadecimalDate = this.hexadecimal(this.getGUIDDate(), 16);
sexadecimalTime = this.hexadecimal(this.getGUIDTime(), 16);
for (var i = 0; i < 9; i++) {
guidStr += Math.floor(Math.random() * 16).toString(16);
}
guidStr += sexadecimalDate;
guidStr += sexadecimalTime;
while (guidStr.length < 32) {
guidStr += Math.floor(Math.random() * 16).toString(16);
}
return this.formatGUID(guidStr);
}

获取当前日期的GUID格式,即8位数的日期:

GUID.prototype.getGUIDDate = function () {
return this.date.getFullYear() + this.addZero(this.date.getMonth() + 1) + this.addZero(this.date.getDay());
}

获取当前时间的GUID格式,即8位数的时间,包括毫秒,毫秒为2位数:12300933

返回GUID日期格式的字条串

GUID.prototype.getGUIDTime = function () {
return this.addZero(this.date.getHours()) + this.addZero(this.date.getMinutes()) + this.addZero(this.date.getSeconds()) + this.addZero(parseInt(this.date.getMilliseconds() / 10));
}

为一位数的正整数前面添加0,如果是可以转成非NaN数字的字符串也可以实现

参数表示准备再前面添加0的数字或可以转换成数字的字符串

如果符合条件,返回添加0后的字条串类型,否则返回自身的字符串

GUID.prototype.addZero = function (num) {
if (Number(num).toString() != ‘NaN’ && num >= 0 && num < 10) {
return ‘0’ + Math.floor(num);
} else {
return num.toString();
}
}

将y进制的数值,转换为x进制的数值

1个参数表示欲转换的数值;第2个参数表示欲转换的进制;第3个参数可选,表示当前的进制数,如不写则为10

返回转换后的字符串

GUID.prototype.hexadecimal = function (num, x, y) {
if (y != undefined) {
return parseInt(num.toString(), y).toString(x);
} else {
return parseInt(num.toString()).toString(x);
}
}

格式化32位的字符串为GUID模式的字符串

第1个参数表示32位的字符串
   标准GUID格式的字符串

GUID.prototype.formatGUID = function (guidStr) {
var str1 = guidStr.slice(0, 8) + ‘-‘,
str2 = guidStr.slice(8, 12) + ‘-‘,
str3 = guidStr.slice(12, 16) + ‘-‘,
str4 = guidStr.slice(16, 20) + ‘-‘,
str5 = guidStr.slice(20);
return str1 + str2 + str3 + str4 + str5;
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781