function emptyFun(obj){
var obj=obj;
if(obj==""||obj==null||obj==undefined||obj=="null"||obj=="undefined"){
return true;
}else{
return false;
}
}
function isObj(obj){
if(typeof obj=="object"){
return true;
}else{
return false;
}
}
function isNumber(str){
var n = Number(str);
if (!isNaN(n)){
return true;
}else{
return false;
}
}
function formattime(number, format) {
if (number != null) {
var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];//
var returnArr = [];
var date = new Date(number);
returnArr.push(date.getFullYear());
returnArr.push(formatNumber(date.getMonth() + 1));
returnArr.push(formatNumber(date.getDate()));
returnArr.push(formatNumber(date.getHours()));
returnArr.push(formatNumber(date.getMinutes()));
returnArr.push(formatNumber(date.getSeconds()));
for (var i in returnArr) {
format = format.replace(formateArr[i], returnArr[i]);
}
//format.replace(/\//g,'-');
return format.replace(/\//g, '-');
} else {
return number;
}
}
/**
* 日期转换
* @param date 日期对象
* @param format 格式化文本
YYYY 年
MM 月
DD 日
HH 小时
ii 分钟
ss 秒
*/
const formatDate = (date, format) => {
format = 'undefined'===typeof symbol?'YYYY-MM-DD HH:ii:ss':format;
format = format.replace(/YYYY/gi, date.getFullYear());
format = format.replace(/MM/gi, formatNumber(date.getMonth() + 1));
format = format.replace(/DD/gi, formatNumber(date.getDate()));
format = format.replace(/HH/gi, formatNumber(date.getHours()));
format = format.replace(/ii/gi, formatNumber(date.getMinutes()));
format = format.replace(/ss/gi, formatNumber(date.getSeconds()));
return format
}
//示例:
formatDate(new Date(), 'YYYY年MM月DD日 HH:ii');
const json2url = json => {
var _keys = Object.keys(json);
var _urlArr = [];
for(var i in _keys){
_urlArr.push(_keys[i]+'='+json[_keys[i]]);
}
return _urlArr.join("&");
}
//示例:
let _data = {
id: 1,
name: 'test'
}
json2url(_data); //输出为:id=1&name=test
const getUrlVars = (url) => {
var hash;
var myJson = {};
var hashes = url.split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
myJson[hash[0]] = hash[1];
}
return myJson;
}
//示例
let _url = 'id=1&name=test';
getUrlVars(_url);
/** 输出结果
{
id: 1,
name: 'test'
}
*/
/**
* 倒计时功能
* @param _stamp 剩余时间戳
* @param _format 输出格式
*/
const getLastTime = (_stamp, _format) => {
_format = 'undefined'===typeof symbol?'DD日 HH:ii:ss':format;
let _currentDate = new Date(),
_endDate = new Date(_stamp),
_cha = _endDate.getTime() - _currentDate.getTime();
if(_cha<=0){
return null;
}
var _day = Math.floor(_cha/86400); //计算剩余天数
var _hours = Math.floor(_cha%86400/3600); //计算剩余小时数
var _minutes = Math.floor(_cha%86400%3600/60); //计算剩余的分钟数
var _seconds = Math.floor(_cha%86400%3600%60); //计算剩余的秒数
//
_format = _format.replace(/DD/gi, formatNumber(_day));
_format = _format.replace(/HH/gi, formatNumber(_hours));
_format = _format.replace(/ii/gi, formatNumber(_minutes));
_format = _format.replace(/ss/gi, formatNumber(_seconds));
return _format;
}
//示例:
getLastTime(1627961116034, 'HH:ii:ss'); //输出 01:20:30
- 通过parseInt计算倒计器和Promise返回结束
/**
* 倒计时
* @param _date 日期对象
* @param _format 格式化文本
*/
const clacLastTime = (_date, _format) => {
_format = 'undefined'===typeof _format?'DD日 HH:ii:ss':_format;
let _startStamp = new Date().getTime(),
_endStamp = _date.getTime(),
_cha = (_endStamp - _startStamp) / 1000,
_resultStr = '';
var day = parseInt(_cha / 60 / 60 / 24, 10); //计算剩余天数
var hour = parseInt(_cha / 60 / 60 % 24, 10); //计算剩余的小时数
var minute = parseInt(_cha / 60 % 60, 10); //计算剩余的分钟数
var second = parseInt(_cha % 60, 10); //计算剩余的秒数
//
_format = _format.replace(/DD/gi, formatNumber(_day));
_format = _format.replace(/HH/gi, formatNumber(_hours));
_format = _format.replace(/ii/gi, formatNumber(_minutes));
_format = _format.replace(/ss/gi, formatNumber(_seconds));
return new Promise((resolve, reject) => {
if(_cha<0){
reject();
}else{
resolve(_format);
}
});
}
//示例
clacLastTime(1627961116034, 'HH:ii:ss').then(res => {
console.log(res); //输出:01:20:30,每秒输入一次结果
}).catch(() => {
console.log('倒计时结束');
});
/**
* 清除两边空格
*/
const trimStr = v => {
return 'string' === typeof v ? v.replace(/(^\s+)|(\s+$)/g, "") : v;
}
//示例:
trimStr(' test '); //输出:test
/**
* 截取字符串部分字符转换为*号
* @param value 被替换的字符串
* @param start 开始位置
* @param end 结束位置
* @param symbol 需替换成的字符符号
*/
const truncateStr = (value, start, end, symbol) => {
if('string'!==typeof value) throw 'Parameter value must be a string';
//检测字符串和范围有校性
if(!(value.length>start&&end>start)) return value;
return value.split('').map((word, i) => { return i>=start&&i<=end?symbol:word }).join('');
}
//示例:
truncateStr('632323190605260287', 5, 13, '*'); //输出63232*********0287
/**
* px转rpx
* @param {*} _val
*/
const px2rpx = _val => {
let _sysInfo = wx.getSystemInfoSync();
return parseFloat((parseInt(_val) * 750 / _sysInfo['windowWidth']).toFixed(2));
}
/**
* rpx转为px
* @param _val
*/
const rpx2px = _val => {
let _sysInfo = wx.getSystemInfoSync();
return parseFloat((parseInt(_val) / 750 * _sysInfo['windowWidth']).toFixed(2));
}