Skip to content

Instantly share code, notes, and snippets.

View lanqy's full-sized avatar
🎯
Focusing

Lan Qingyong lanqy

🎯
Focusing
  • Shenzhen,China
View GitHub Profile
@lanqy
lanqy / fulldate.js
Created February 16, 2013 06:12
时间戳转换成类似2012-12-20 的时间格式
_fullDate: function(time){ //转换成类似 2012-12-20 的时间格式
var date = new Date(time);
return date.getFullYear() + '-' + (date.getMonth() >= 9 ? '' : '0') + (date.getMonth() + 1) + '-' + (date.getDate() > 9 ? '' : '0') + date.getDate();
},
/**
* IE9表格渲染bug修复,表格数据量大时,ie9下莫名其妙出现空的td
*/
IE9TabelRenderFixed: function(str){
var reg = new RegExp('>[ \t\r\n\v\f]*<', 'g');
return str.replace(reg, '><');
}
@lanqy
lanqy / hex2rgb.js
Created February 16, 2013 07:53
Hex to RGB or RGBA transluder
// from http://snipplr.com/view/43542/
function hex2rgb(hex, opacity) {
var rgb = hex.replace('#', '').match(/(.{2})/g);
var i = 3;
while (i--) {
rgb[i] = parseInt(rgb[i], 16);
}
if (typeof opacity == 'undefined') {
@lanqy
lanqy / gist:4975044
Last active December 13, 2015 21:08
sort
//http://stackoverflow.com/questions/979256/how-to-sort-an-array-of-javascript-objects
// demo http://jsfiddle.net/dFNva/1/
var sort_by = function(field, reverse, primer){
var key = function (x) {return primer ? primer(x[field]) : x[field]};
return function (a,b) {
var A = key(a), B = key(b);
return ((A < B) ? -1 :
(A > B) ? +1 : 0)) * [-1,1][+!!reverse];
}
}
@lanqy
lanqy / date2Age.js
Last active December 14, 2015 13:58
// from http://stackoverflow.com/questions/5786186/javascript-age-count-from-date-of-birth
function date2Age(str){ // 根据年月日计算年龄str 格式为yyyy-mm-dd
var diff = new Date - new Date(parseISO8601(str));
var diffdays = diff / 1000 / (60 * 60 * 24);
return Math.floor(diffdays / 365.25)
}
//form http://stackoverflow.com/questions/2182246/javascript-dates-in-ie-nan-firefox-chrome-ok
function parseISO8601(dateStringInRange) { // fixed IE8 NaN
var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
date = new Date(NaN),
@lanqy
lanqy / bytesToSize.js
Created March 19, 2013 03:05
JavaScript To Convert Bytes To MB, KB, Etc
// from http://scratch99.com/web-development/javascript/convert-bytes-to-mb-kb/
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
if (i == 0) return bytes + ' ' + sizes[i];
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};
@lanqy
lanqy / _encodeHTML.js
Created March 22, 2013 01:18
转换特殊字符
_encodeHTML: function(source){ //转换特殊字符
return source.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, "&quot;")
.replace(/\(/g, "&#40;")
.replace(/\)/g, "&#41;")
.replace(/\$/g, "&#36;")
.replace(/'/g, "&#39;");
}
@lanqy
lanqy / Plugin_Pattern.js
Last active December 15, 2015 11:29
jQuery Plugin Pattern
(function($){
$.extend($.fn,{
pluginName: function(options){
return this.each(function(){
var pluginName = $.data(this, "pluginName");
if(!pluginName){
pluginName = new $.pluginName(options, this);
$.data(this, "pluginName", pluginName);
@lanqy
lanqy / randomMobileNum.js
Created March 28, 2013 03:06
随机生成20w号码
var mobiles = [];
for (var i = 0; i < 200000; i++) {
mobiles[i] = (13000000000 + Math.floor(Math.random() * 1000000000)).toString();
}
@lanqy
lanqy / removeHtmlTag.js
Created April 9, 2013 01:17
remove HTML tag
function removeHtmlTag(str){
var reg = /(<([^>]+)>)/ig;
return str.replace(reg, "");
}