182 lines
4.7 KiB
JavaScript
182 lines
4.7 KiB
JavaScript
function formatTime(time) {
|
||
if (typeof time !== 'number' || time < 0) {
|
||
return time
|
||
}
|
||
|
||
var hour = parseInt(time / 3600)
|
||
time = time % 3600
|
||
var minute = parseInt(time / 60)
|
||
time = time % 60
|
||
var second = time
|
||
|
||
return ([hour, minute, second]).map(function(n) {
|
||
n = n.toString()
|
||
return n[1] ? n : '0' + n
|
||
}).join(':')
|
||
}
|
||
|
||
function formatLocation(longitude, latitude) {
|
||
if (typeof longitude === 'string' && typeof latitude === 'string') {
|
||
longitude = parseFloat(longitude)
|
||
latitude = parseFloat(latitude)
|
||
}
|
||
|
||
longitude = longitude.toFixed(2)
|
||
latitude = latitude.toFixed(2)
|
||
|
||
return {
|
||
longitude: longitude.toString().split('.'),
|
||
latitude: latitude.toString().split('.')
|
||
}
|
||
}
|
||
var dateUtils = {
|
||
UNITS: {
|
||
'年': 31557600000,
|
||
'月': 2629800000,
|
||
'天': 86400000,
|
||
'小时': 3600000,
|
||
'分钟': 60000,
|
||
'秒': 1000
|
||
},
|
||
humanize: function(milliseconds) {
|
||
var humanize = '';
|
||
for (var key in this.UNITS) {
|
||
if (milliseconds >= this.UNITS[key]) {
|
||
humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
|
||
break;
|
||
}
|
||
}
|
||
return humanize || '刚刚';
|
||
},
|
||
format: function(dateStr) {
|
||
var date = this.parse(dateStr)
|
||
var diff = Date.now() - date.getTime();
|
||
if (diff < this.UNITS['天']) {
|
||
return this.humanize(diff);
|
||
}
|
||
var _format = function(number) {
|
||
return (number < 10 ? ('0' + number) : number);
|
||
};
|
||
return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
|
||
_format(date.getHours()) + ':' + _format(date.getMinutes());
|
||
},
|
||
parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
|
||
var a = str.split(/[^0-9]/);
|
||
return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
|
||
}
|
||
};
|
||
/**
|
||
*对Date的扩展,将 Date 转化为指定格式的String
|
||
*月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
|
||
*年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
|
||
*例子:
|
||
*(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
|
||
*(new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
|
||
*/
|
||
Date.prototype.Format = function(fmt) { //author: meizz
|
||
var o = {
|
||
"M+": this.getMonth() + 1, //月份
|
||
"d+": this.getDate(), //日
|
||
"H+": this.getHours(), //小时
|
||
"m+": this.getMinutes(), //分
|
||
"s+": this.getSeconds(), //秒
|
||
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
|
||
"S": this.getMilliseconds() //毫秒
|
||
};
|
||
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
|
||
for (var k in o)
|
||
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[
|
||
k]).substr(("" + o[k]).length)));
|
||
return fmt;
|
||
}
|
||
Array.prototype.indexOf = function(val) {
|
||
for (var i = 0; i < this.length; i++) {
|
||
if (this[i] == val) {
|
||
return i;
|
||
}
|
||
}
|
||
return -1;
|
||
}
|
||
Array.prototype.remove = function(val) {
|
||
var index = this.indexOf(val);
|
||
if (index > -1) {
|
||
this.splice(index, 1);
|
||
}
|
||
}
|
||
String.prototype.Trim = function() {
|
||
return this.replace(/(^\s*)|(\s*$)/g, "");
|
||
}
|
||
String.prototype.LTrim = function() {
|
||
return this.replace(/(^\s*)/g, "");
|
||
}
|
||
String.prototype.RTrim = function() {
|
||
return this.replace(/(\s*$)/g, "");
|
||
}
|
||
|
||
function formatNumber(n) {
|
||
n = n.toString()
|
||
return n[1] ? n : '0' + n
|
||
}
|
||
|
||
function objectToUrlParams(obj) {
|
||
var str = "";
|
||
for (var key in obj) {
|
||
str += "&" + key + "=" + obj[key];
|
||
}
|
||
return str.substr(1);
|
||
}
|
||
|
||
function isWechatOrQQ() {
|
||
var ua = navigator.userAgent.toLowerCase();
|
||
if (ua.match(/MicroMessenger/i) == "micromessenger" || ua.match(/WeiBo/i) == "weibo") {
|
||
return true;
|
||
} else if (ua.indexOf('mobile mqqbrowser') > -1) {
|
||
return true;
|
||
} else if (ua.indexOf('iphone') > -1 || ua.indexOf('mac') > -1) {
|
||
if (ua.indexOf('qq') > -1) {
|
||
return true;
|
||
}
|
||
|
||
}
|
||
return false;
|
||
}
|
||
|
||
function getDate(type) {
|
||
const date = new Date();
|
||
let year = date.getFullYear();
|
||
let month = date.getMonth() + 1;
|
||
let day = date.getDate();
|
||
if (type === 'start') {
|
||
year = year - 60;
|
||
} else if (type === 'end') {
|
||
year = year + 2;
|
||
}
|
||
month = month > 9 ? month : '0' + month;
|
||
day = day > 9 ? day : '0' + day;
|
||
return `${year}-${month}-${day}`;
|
||
}
|
||
|
||
/*获取当前时间*/
|
||
function getNowTime() {
|
||
return Math.round(new Date().getTime() / 1000)
|
||
}
|
||
|
||
/*是否H5*/
|
||
function isH5() {
|
||
// #ifdef H5
|
||
return true;
|
||
// #endif
|
||
return false;
|
||
}
|
||
|
||
module.exports = {
|
||
formatTime: formatTime,
|
||
formatLocation: formatLocation,
|
||
dateUtils: dateUtils,
|
||
objectToUrlParams: objectToUrlParams,
|
||
isWechatOrQQ: isWechatOrQQ,
|
||
getDate: getDate,
|
||
getNowTime: getNowTime,
|
||
isH5: isH5
|
||
}
|