12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javascript で uuid

Last updated at Posted at 2014-03-24
	$.fn.uuid = function() {
		this.each(function(i,element) {
			;
			var _uuid = "";
			var _dobj = new Date();
			var _yy = (_dobj.getFullYear()).toString(16).substring(1,3); // yyyy
			var _mm = "0" + (255 - _dobj.getMonth()    ).toString(16); //  mm
			var _dd = "0" + (252 - _dobj.getDate()     ).toString(16); // dd
			var _hh =       (240 - _dobj.getHours()    ).toString(16); // hh
			var _mi =       (192 - _dobj.getMinutes()  ).toString(16); // mi
			var _ss =       ( 16 + _dobj.getSeconds()  ).toString(16); // ss
			var _r1 = Math.pow(Math.random()+1, 3) % 1;
			var _r2 = Math.pow(Math.random()+1, 3) % 1;
			var _d1 = Math.floor(_r1 * Math.pow(16,4)).toString(16); 
			while (_d1.length < 4 ) _d1 = "0" + _d1;
			var _d2 = Math.floor(_r2 * Math.pow(16,12)).toString(16); 
			while (_d2.length < 12 ) _d2 = "0" + _d2;
			
			_uuid += _yy + _mm + _dd + "-" + _hh + _mi + "-" + _ss + "00" 
			      + "-" + _d1 + "-" + _d2;
			$(element).data("uuid" , _uuid);
			console.log(_uuid);
		});
		return this;
	};

参考

name calc ex.: 2014/3/24 10:20:30
_yy (年).toString(16).substring(1,2) de
_mm (255-(月)).toString(16) fd
_dd (252-(日)).toString(16) e4
_hh (240-(時)).toString(16) e6
_mi (192-(分)).toString(16) ac
_ss (16+(秒)).toString(16) 2e
_r1 乱数1 0af6
_r2 乱数2 216da69ff61c
_uuid 結果 de0fd0e4-e6ac-2e00-0af6-216da69ff61c
12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?