格式化时间:
function formatDateTime(timeStamp){
var date = new Date(timeStamp);
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();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
}
把单位为秒的时间,改成一般时间:
function formatSecond(second){
var h = Math.floor(second / 3600);
h = h < 10 ? ('0' + h) : h;
var minute = Math.floor(second / 60);
minute = minute < 10 ? ('0' + minute) : minute;
var second = Math.floor(second % 60);
second = second < 10 ? ('0' + second) : second;
return h != "00" ? (h+“:”+minute":"+second) : (minute+":"+second;
}
对比时间,并返回“几天前”或“几分钟前”等:
(注:该方法在IOS系统中无效,若想要同时兼容IOS和Android,则需要引入第三方插件moment.js)
function getTime(date){
var now = new Date();
var ctime = new Date(date);
//var now = moment(); //兼容IOS和Android
// var ctime = moment(date);//兼容IOS和Android
// var date3= now -ctime; //兼容IOS和Android 时间差的毫秒数
var date3=now.getTime()-ctime.getTime() //时间差的毫秒数
//计算出相差天数
var days=Math.floor(date3/(24*3600*1000));
if(days == 1){
return "昨天";
}else if(days == 2){
return "前天";
}else if(days > 2 && days < 31){
return days+"天前";
}else if(days >= 31 && days <= 2*31){
return "一个月前";
}else if(days > 2* 31 && days <= 3*31){
return "2个月前";
}else if(days > 3* 31 && days <= 4*31){
return "3个月前";
}
//计算出小时数
var leave1=date3%(24*3600*1000); //计算天数后剩余的毫秒数
var hours = Math.floor(leave1/(3600*1000));
if(hours > 0)return hours+"小时前"
//计算相差分钟数
var leave2=leave1%(3600*1000); //计算小时数后剩余的毫秒数
var minutes=Math.floor(leave2/(60*1000));
if(minutes > 0)return minutes+"分钟前"
if(minutes == 0)return "1分钟前";
//计算相差秒数
var leave3=leave2%(60*1000); //计算分钟数后剩余的毫秒数
var seconds=Math.round(leave3/1000);
if(seconds > 0)return seconds+"秒前"
// 日期格式化
return date;
}
查询参数:
function getQueryString(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return unescape(r[2]); return null;
}
判断设备类型:
function isAS() {
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/iPhone\sOS/i) == "iphone os") {
return 1;
} else if (ua.match(/Android/i) == "android") {
return 2;
}
}
关于cookie:
let $cookie = {
getCookie: function(t) {
return null == document.cookie.match(RegExp("(^" + t + "| " + t + ")=([^;]*)")) ? "" : decodeURIComponent(RegExp.$2)
},
setCookie: function(t, e, i) {
i = new Date((new Date).getTime() + 36e5 * i),
document.cookie = t + "=" + escape(e) + "; path=/; expires=" + i.toGMTString()
},
delCookie: function(t) {
$cookie.setCookie(t, "", -1)
},
getVersionCookie: function(name){
let strcookie = document.cookie;//获取cookie字符串
let r = new RegExp(name+'=([^,]*)');
if(strcookie.match(r)) {
return strcookie.match(r)[1]
}
return '';
}
}
ajax请求:
var md5 = require("./md5")
var http = {
get: function(url, data, success, error) {
var self = this;
$.ajax({
type: "GET",
url: url,
data: data,
dataType: "json",
headers: self._checkFun(data),
success: function (res) {
success && success(res);
},
error: function(err) {
error && error();
}
})
},
post: function(url, data, success, error) {
var self = this;
$.ajax({
type: "POST",
url: url,
data: data,
dataType: "json",
headers: self._checkFun(data),
success: function (res) {
success && success(res);
},
error: function(err) {
error && error();
}
})
},
_checkFun: function(data){
let sign = [];
let salt = '12312314232';
let timeStamp = Date.now();
sign.push(salt);
sign.push(timeStamp);
for (let i in data) {
sign.push(data[i])
}
sign = sign.sort();
sign = md5.hex_md5(sign.join(''));
return {timeStamp, sign}
},
}