guid.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. function GUID() {
  2.   this.date = new Date();
  3.  
  4.   /* 判断是否初始化过,如果初始化过以下代码,则以下代码将不再执行,实际中只执行一次 */
  5.   if (typeof this.newGUID != 'function') {
  6.      
  7.     /* 生成GUID码 */
  8.     GUID.prototype.newGUID = function() {
  9.       this.date = new Date();
  10.       var guidStr = '';
  11.         sexadecimalDate = this.hexadecimal(this.getGUIDDate(), 16);
  12.         sexadecimalTime = this.hexadecimal(this.getGUIDTime(), 16);
  13.       for (var i = 0; i < 9; i++) {
  14.         guidStr += Math.floor(Math.random()*16).toString(16);
  15.       }
  16.       guidStr += sexadecimalDate;
  17.       guidStr += sexadecimalTime;
  18.       while(guidStr.length < 32) {
  19.         guidStr += Math.floor(Math.random()*16).toString(16);
  20.       }
  21.       return this.formatGUID(guidStr);
  22.     }
  23.  
  24.     /*
  25.      * 功能:获取当前日期的GUID格式,即8位数的日期:19700101
  26.      * 返回值:返回GUID日期格式的字条串
  27.      */
  28.     GUID.prototype.getGUIDDate = function() {
  29.       return this.date.getFullYear() + this.addZero(this.date.getMonth() + 1) + this.addZero(this.date.getDay());
  30.     }
  31.  
  32.     /*
  33.      * 功能:获取当前时间的GUID格式,即8位数的时间,包括毫秒,毫秒为2位数:12300933
  34.      * 返回值:返回GUID日期格式的字条串
  35.      */
  36.     GUID.prototype.getGUIDTime = function() {
  37.       return this.addZero(this.date.getHours()) + this.addZero(this.date.getMinutes()) + this.addZero(this.date.getSeconds()) + this.addZero( parseInt(this.date.getMilliseconds() / 10 ));
  38.     }
  39.  
  40.     /*
  41.     * 功能: 为一位数的正整数前面添加0,如果是可以转成非NaN数字的字符串也可以实现
  42.      * 参数: 参数表示准备再前面添加0的数字或可以转换成数字的字符串
  43.      * 返回值: 如果符合条件,返回添加0后的字条串类型,否则返回自身的字符串
  44.      */
  45.     GUID.prototype.addZero = function(num) {
  46.       if (Number(num).toString() != 'NaN' && num >= 0 && num < 10) {
  47.         return '0' + Math.floor(num);
  48.       } else {
  49.         return num.toString();
  50.       }
  51.     }
  52.  
  53.     /*
  54.      * 功能:将y进制的数值,转换为x进制的数值
  55.      * 参数:第1个参数表示欲转换的数值;第2个参数表示欲转换的进制;第3个参数可选,表示当前的进制数,如不写则为10
  56.      * 返回值:返回转换后的字符串
  57.      */
  58.     GUID.prototype.hexadecimal = function(num, x, y) {
  59.       if (y != undefined) {
  60.         return parseInt(num.toString(), y).toString(x);
  61.       } else {
  62.         return parseInt(num.toString()).toString(x);
  63.       }
  64.     }
  65.  
  66.     /*
  67.      * 功能:格式化32位的字符串为GUID模式的字符串
  68.      * 参数:第1个参数表示32位的字符串
  69.      * 返回值:标准GUID格式的字符串
  70.      */
  71.     GUID.prototype.formatGUID = function(guidStr) {
  72.       var str1 = guidStr.slice(0, 8) + '-',
  73.         str2 = guidStr.slice(8, 12) + '-',
  74.         str3 = guidStr.slice(12, 16) + '-',
  75.         str4 = guidStr.slice(16, 20) + '-',
  76.         str5 = guidStr.slice(20);
  77.       return str1 + str2 + str3 + str4 + str5;
  78.     }
  79.   }
  80. }