Last active
May 3, 2024 16:39
-
-
Save webkong/984abb89b0bbbb99b9bd9870c75bcd14 to your computer and use it in GitHub Desktop.
[JavaScript]snippets for JavaScript #JavaScript
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
/** | |
* @description 将时间转换成指定格式 | |
* @param {String|Number} time | |
* @param {String} fmt | |
*/ | |
export function timesToDate(time, fmt) { | |
fmt = fmt || 'yyyy-MM-dd'; | |
time = anyTotime(time); | |
let date = new Date(time); | |
if (/(y+)/.test(fmt)) { | |
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); | |
} | |
let o = { | |
'M+': date.getMonth() + 1, | |
'd+': date.getDate(), | |
'h+': date.getHours(), | |
'm+': date.getMinutes(), | |
's+': date.getSeconds() | |
}; | |
for (let k in o) { | |
let str = o[k] + ''; | |
if (new RegExp(`(${k})`).test(fmt)) { | |
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str)); | |
} | |
} | |
return fmt; | |
} | |
/** | |
* | |
* @param {String|Number} time | |
*/ | |
export function getAstro(time) { | |
time = anyTotime(time); | |
let date = new Date(time); | |
let month = date.getMonth() + 1; | |
let day = date.getDate(); | |
let s = "魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯"; | |
let arr = [20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22]; | |
return s.substr(month * 2 - (day < arr[month - 1] ? 2 : 0), 2); | |
} | |
/** | |
* @description 返回从出生到现在的天数 | |
* @param {String|Number} time | |
*/ | |
export function birthDays(time) { | |
time = anyTotime(time); | |
return parseInt((new Date().getTime() - new Date(time).getTime()) / 86400000); //(1000 * 60 * 60 *24) | |
} | |
/** | |
* @description 把传入任意格式的时间,转化成(毫秒)时间戳 | |
* @param {String|Number} time | |
*/ | |
export function anyTotime(time) { | |
if (typeof time !== 'number') { | |
time = Date.parse(new Date(time).toString()); | |
} | |
time = time.toString(); | |
if (time.length === 10) { | |
time = time * 1000; | |
} else { | |
time = time * 1; | |
} | |
return time; | |
} | |
function padLeftZero(str) { | |
return ('00' + str).substr(str.length); | |
} |
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
/** | |
* | |
* @param {*Array} arg | |
*/ | |
module.exports = function (arg) { | |
if (typeof arg === 'object') { | |
return Object.prototype.toString.call(arg) === '[object Array]'; | |
} | |
return false; | |
} |
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
/** | |
* | |
* @param {*String} url | |
* @param {*string} target | |
*/ | |
module.exports = function(url, target) { | |
var a = document.createElement("a"); | |
a.setAttribute("href", url); | |
a.setAttribute("target", target); | |
document.body.appendChild(a); | |
a.click(); | |
a.remove(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very useful!collect it