Fork me on GitHub

常用js方法

工作中常用的js工具方法总结记录下来,持续更新中……

秒数转换为时分秒

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function sndToTime(value) {
var length = Math.floor(parseInt(value));
var hour = Math.floor(value / 3600);
var minute = Math.floor(value / 60);
if (minute < 10) {
minute = "0" + minute;
}
var second = length % 60;
if (second < 10) {
second = "0" + second;
}
if (hour > 0 && hour < 10) {
minute = Math.floor((value - hour * 3600) / 60);
hour = '0' + hour;
if (minute < 10) {
minute = "0" + minute;
}
return hour + ':' + minute + ":" + second;
} else {
return minute + ":" + second;
}
}

指定日期与当前日期时间差计算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function timeAgo(dateTimeStamp) {
let timetamp = dateTimeStamp.replace(/\-/g, "/")
let minute = 1000 * 60
let hour = minute * 60
let day = hour * 24
let month = day * 30
let now = new Date().getTime()
let diffValue = now - (parseInt(Date.parse(new Date(timetamp))))
if (diffValue < 0) {
// alert('结束日期不能小于开始日期!')
}
let date = new Date(timetamp)
let Y = date.getFullYear()
let M = date.getMonth() + 1
let D = date.getDate()
let H = date.getHours()
let m = date.getMinutes()
let s = date.getSeconds()
//小于10的在前面补0
if (M < 10) {
M = '0' + M;
}
if (D < 10) {
D = '0' + D;
}
if (H < 10) {
H = '0' + H;
}
if (m < 10) {
m = '0' + m;
}
if (s < 10) {
s = '0' + s;
}
let hourC = diffValue / hour
let minC = diffValue / minute
let result
let nowTime = new Date();
let nowYear = nowTime.getFullYear();
let nowMonth = nowTime.getMonth() + 1;
let nowDay = nowTime.getDate();
if (parseInt(Y) < nowYear) {
result = `${Y}${M}${D}日`;
} else if (parseInt(M) < nowMonth) {
result = `${M}${D}日`;
} else if (parseInt(D) < nowDay - 2) {
result = `${M}${D}日`;
} else if (parseInt(D) == nowDay - 2) {
result = `前天 ${H}:${m}`;
} else if (parseInt(D) == nowDay - 1) {
result = `昨天 ${H}:${m}`;
} else if (hourC >= 1 && hourC < 24) {
result = parseInt(hourC) + '个小时前'
} else if (minC >= 1 && hourC < 1) {
result = parseInt(minC) + '分钟前'
} else if (minC < 1) {
result = '刚刚'
}
return result
}

提取链接中的字段

1
2
3
4
5
6
function getQuerys(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return r[2];
return null;
}

转化文件显示大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function filterSize(b) {
b = parseFloat(b);
if (!b) {
return '0B';
}
if (b < 1024) {
return b + 'B';
}
const kb = (b / 1024).toFixed(2);
if (kb < 1024) {
return kb + 'KB';
}
const m = (kb / 1024).toFixed(2);
if (m > 1024) {
const g = (m / 1024).toFixed(2);
return g + 'G';
} else {
return m + 'M';
}
}

cookie设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const cookier = {
set: function (key, value, hours, domain) {
let cookieStr = '';
cookieStr = `${key}=${value};`;
if (hours) {
let exp = new Date();
exp.setTime(exp.getTime() + hours * 60 * 60 * 1000);
cookieStr += `expires=${exp.toGMTString()};`;
}
if (domain) {
cookieStr += `domain=${domain};`;
}
document.cookie = cookieStr;
},
get: function (name) {
let reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
if (document.cookie.match(reg)) {
if (document.cookie.match(reg)[2] === undefined || document.cookie.match(reg)[2] === 'undefined' || document.cookie.match(reg)[2] == 'null') {
return null;
} else {
return document.cookie.match(reg)[2];
}
} else {
return null;
}
},
del: function (name) {
const value = this.get(name);
if (!value) {
return;
}
let exp = new Date();
exp.setTime(exp.getTime() - 1);
document.cookie = `${name}=;expires=${exp.toGMTString()}`;
}
}

日期转换为年月日时分秒

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function dateToTime(date) {
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
minute = minute < 10 ? ('0' + minute) : minute;
var second = date.getSeconds();
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
}

对象数组按照属性排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function sortObjectArray(sortUp, arr, prop1, prop2) {
let compare = function (prop1, prop2) {
return function (obj1, obj2) {
let val1 = obj1[prop1];
let val2 = obj2[prop1];
if (val1 < val2) {
return sortUp === 'des' ? 1 : -1;
} else if (val1 > val2) {
return sortUp === 'des' ? -1 : 1;
} else {
if (prop2) {
let val3 = obj1[prop2];
let val4 = obj2[prop2];
if (val3 < val4) {
return sortUp === 'des' ? 1 : -1;
} else if (val3 > val4) {
return sortUp === 'des' ? -1 : 1;
}
} else {
return 0;
}
}
}
}
return arr.sort(compare(prop1, prop2))
}

禁止页面鼠标右键和复制、粘贴事件

1
2
3
4
5
6
7
8
9
10
11
12
document.oncontextmenu = function (e) { return false; };
document.oncopy = function (e) { return false; };
// 禁用Ctrl+C和Ctrl+V(所有浏览器均支持)
$(document).keydown(function (e) {
if (e.ctrlKey && (e.keyCode == 65 || e.keyCode == 67)) {
return false;
}
});
//禁止文字选中
document.body.onselectstart = document.body.ondrag = function () {
return false;
}

判断是在PC端还是在手机端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function IsPC() {
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}