Last active
June 27, 2019 07:18
-
-
Save davinma/6b4e4658028d7cee2376b77cbbc1e752 to your computer and use it in GitHub Desktop.
JS common utilities.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 检测 WebGL 是否启用 | |
* 参考 http://www.browserleaks.com/webgl#howto-detect-webgl | |
* 参考 https://gist.github.com/SeanZoR/cfa7a6206983b775a858 | |
* | |
* @return { number } | |
* -1: 不持支 WebGL | |
* 0: WebGL 被禁用 | |
* 1: WebGL 已启用 | |
*/ | |
var detectWebGL = function () { | |
// 检测 WebGL 渲染 context | |
if (!!window.WebGLRenderingContext) { | |
var canvas = document.createElement('canvas'), | |
names = ['webgl', 'experimental-webgl', 'moz-webgl', 'webkit-3d'], | |
context = false; | |
for (var i in names) { | |
try { | |
context = canvas.getContext(names[i]); | |
if (context && typeof context.getParameter === 'function') { | |
// WebGL 已启用 | |
return 1; | |
} | |
} catch (e) {} | |
} | |
// 持支 WebGL 但被禁用 | |
return 0; | |
} | |
// 不持支 WebGL | |
return -1; | |
}; | |
/** | |
* 动态加载脚本 | |
* @param url 脚本地址 | |
* @param callback 加载完成后回调 | |
*/ | |
var loadScript = function (url, callback) { | |
var script = document.createElement('script'); | |
if (script.readyState) { | |
// IE | |
script.onreadystatechange = function () { | |
if (script.readyState === 'loaded' || script.readyState === 'complete') { | |
script.onreadystatechange = null; | |
callback(); | |
} | |
}; | |
} else { | |
// 非 IE | |
script.onLoad = callback; | |
} | |
script.src = url; | |
document.getElementsByTagName('body')[0].appendChild(script); | |
}; | |
/** | |
* 根据日期计算年龄 | |
* @param dateString 日期字符串 | |
*/ | |
var getAge = function (dateString) { | |
var birthday = +new Date(dateString); | |
return ~~((Date.now() - birthday) / (31557600000)); | |
}; | |
/** | |
* 日期格式化,格式:YYYY-MM-DD hh:mm | |
* @param time 日期字符串 | |
*/ | |
var formatTime = function (time) { | |
var d = new Date(time), | |
YYYY = d.getFullYear(), | |
MM = ('0' + (d.getMonth() + 1)).slice(-2), | |
DD = ('0' + (d.getDate())).slice(-2), | |
hh = ('0' + (d.getHours())).slice(-2), | |
mm = ('0' + (d.getMinutes())).slice(-2); | |
return YYYY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm; | |
}; | |
/** | |
* 日期格式化,格式:YYYY年MM月 | |
* @param time 日期字符串 | |
*/ | |
var formatMonth = function (time) { | |
var d = new Date(time), | |
YYYY = d.getFullYear(), | |
MM = d.getMonth() + 1; | |
return YYYY + '年' + MM + '月'; | |
}; | |
/** | |
* 日期格式化,格式:YYYY年MM月DD日 | |
* @param time 日期字符串 | |
*/ | |
var formatDay = function (time) { | |
var d = new Date(time), | |
YYYY = d.getFullYear(), | |
MM = ('0' + (d.getMonth() + 1)).slice(-2), | |
DD = ('0' + (d.getDate())).slice(-2); | |
return YYYY + '-' + MM + '-' + DD; | |
}; | |
/** | |
* 日期格式化,格式:MM月DD日 | |
* @param time 日期字符串 | |
*/ | |
var formatTheDay = function (time) { | |
var d = new Date(time), | |
MM = d.getMonth() + 1, | |
DD = d.getDate(); | |
return MM + '月' + DD + '日'; | |
}; | |
/** | |
* 日期格式化,格式:hh:mm | |
* @param time 日期字符串 | |
*/ | |
var formatTheTime = function (time) { | |
var d = new Date(time), | |
hh = ('0' + (d.getHours())).slice(-2), | |
mm = ('0' + (d.getMinutes())).slice(-2); | |
return hh + ':' + mm; | |
}; | |
/** | |
* 日期格式化,根据时间段返回不同的日期描述 | |
* @param time 日期字符串 | |
* | |
* 列如: | |
* 时间跨度为 1 分钟内,返回描述文字 '刚刚'; | |
* 时间跨度为 1 小时内(即 60 分钟),返回描述文字 'mm 分钟前'; | |
* 时间跨度为 1 小时外,但属于今天,返回描述文字 '今天 hh:mm'; | |
* 时间跨度往回推为昨天时,返回描述文字 '昨天 hh:mm'; | |
* 时间跨度为当前年份时,返回描述文字 'MM-DD hh:mm'; | |
* 时间跨度往回推不为当前年份时,返回描述文字 'YYYY-MM-DD hh:mm'; | |
*/ | |
var formatTimeMoments = function (time) { | |
// 当前时间戳秒数 | |
var curTimestamp = parseInt(new Date().getTime() / 1000); | |
// 参数时间戳与当前时间戳相差秒数 | |
var timestampDiff = curTimestamp - time; | |
// 当前时间日期对象 | |
var curDate = new Date(curTimestamp * 1000); | |
// 参数时间戳转换成的日期对象 | |
var tmDate = new Date(time * 1000); | |
var Y = tmDate.getFullYear(), | |
m = tmDate.getMonth() + 1, | |
d = tmDate.getDate(); | |
var H = tmDate.getHours(), | |
i = tmDate.getMinutes(), | |
s = tmDate.getSeconds(); | |
var zeroize = function (num) { | |
return (String(num).length == 1 ? '0' : '') + num; | |
}; | |
if (timestampDiff < 60) { | |
return '刚刚'; | |
} else if (timestampDiff < 3600) { | |
return Math.floor(timestampDiff / 60) + ' 分钟前'; | |
} else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) { | |
return '今天 ' + zeroize(H) + ':' + zeroize(i); | |
} else { | |
var newDate = new Date((curTimestamp - 86400) * 1000); | |
if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) { | |
return '昨天 ' + zeroize(H) + ':' + zeroize(i); | |
} else if (curDate.getFullYear() == Y) { | |
return zeroize(m) + '-' + zeroize(d) + ' ' + zeroize(H) + ':' + zeroize(i); | |
} else { | |
return Y + '-' + zeroize(m) + '-' + zeroize(d) + '-' + zeroize(H) + ':' + zeroize(i); | |
} | |
} | |
}; | |
// 获取 IE 或 Edge 浏览器版本号 | |
var getIEVersion = function () { | |
var ua = window.navigator.userAgent; | |
var msie = ua.indexOf('MSIE '); | |
// IE 10 及已下 | |
if (msie > 0) { | |
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10); | |
} | |
var trident = ua.indexOf('Trident/'); | |
// IE 11 及以上 | |
if (trident > 0) { | |
var rv = ua.indexOf('rv:'); | |
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10); | |
} | |
var edge = ua.indexOf('Edge/'); | |
// Edge (IE 12+) | |
if (edge > 0) { | |
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10); | |
} | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment