eapp
Version:
174 lines (160 loc) • 4.63 kB
JavaScript
/**
* 解决ios手机浏览器时间返回NaN问题
* @param timeStr 如:2018-09-21 17:00:12
* @returns 如:2018/09/21 17:00:12
*/
function initTimeStr(timeStr) {
timeStr = timeStr.replace(/\-/g, "/");
// console.log("timeStr---->" + timeStr);
return timeStr;
}
/**
* 获取智能化显示时间
* @param theTime: 时间戳
* @returns {string} 如:
*
* 25分钟前
* 今天 10:21
* 昨天 13:10
* 前天 15:23
* 09-24 16:01
* 09-23 17:09
*/
function timestampToZNTimeStr(theTime) {
// 时间戳为10位需*1000,时间戳为13位的话不需乘1000
var date = new Date(theTime);
var d = new Date();
var curTime = d.getTime();
// d.setDate(d.getDate());
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
d.setMilliseconds(0);
var todayTime = d.getTime();
var today = d.getDate();
d.setDate(today - 1);
var YesterdayTime = d.getTime();
d.setDate(today - 2);
var beforeTime = d.getTime();
var timeStr = '';
if (theTime > todayTime) {
// console.log("今天 ");
// 单位秒
var cha = (curTime - theTime) / 1000;
// 1小时之内
if (cha <= 3600) {
var t = Math.round(cha / 60);
if(t < 1){
return "刚刚";
}
return t + "分钟前";
} else {
timeStr = '今天 ';
}
} else if (theTime < todayTime && theTime >= YesterdayTime) {
// console.log("昨天 ");
timeStr = '昨天 ';
} else if (theTime < YesterdayTime && theTime >= beforeTime) {
// console.log("前天 ");
timeStr = '前天 ';
} else {
var month = date.getMonth() + 1;
if (month >= 1 && month <= 9) {
month = "0" + month;
}
var strDate = date.getDate();
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
timeStr = month + "-" + strDate + " ";
// console.log(timeStr);
}
var hour = date.getHours();
if (hour >= 0 && hour <= 9) {
hour = "0" + hour;
}
var min = date.getMinutes();
if (min >= 0 && min <= 9) {
min = "0" + min;
}
hour = hour + ':';
return timeStr + hour + min;
}
/**
* 获取时间字符串
* @param date
* @returns {string} 2018-09-07 09:08:07
*/
function getDateAllStr(date) {
if (date == null || date == undefined) {
date = new Date();
}
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
if (month >= 1 && month <= 9) {
month = "0" + month;
}
var strDate = date.getDate();
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var hour = date.getHours();
if (hour >= 0 && hour <= 9) {
hour = "0" + hour;
}
var min = date.getMinutes();
if (min >= 0 && min <= 9) {
min = "0" + min;
}
var sec = date.getSeconds();
if (sec >= 0 && sec <= 9) {
sec = "0" + sec;
}
var currentdate = date.getFullYear() + seperator1
+ month + seperator1
+ strDate + " "
+ hour + seperator2
+ min + seperator2
+ sec;
return currentdate;
}
/**
* Date扩展方法,根据格式化,输出时间字符串
* 如:new Date(chosen).format("yyyy-MM-dd hh:mm:ss");
* @param format "yyyy-MM-dd hh:mm:ss"、"MM-dd hh:mm"、"yyyy-MM-dd hh:mm"
* @returns {*} 2018-10-24 13:30:25
*/
Date.prototype.format = function (format) {
var args = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds()
};
if (/(y+)/.test(format))
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var i in args) {
var n = args[i];
if (new RegExp("(" + i + ")").test(format))
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
}
return format;
};
/**
* 判断是否为空
* @param str
* @returns {boolean}
*/
function isEmpty(str) {
return (str == null || str == '' || str == undefined || str == 'undefined');
}
/**
* 去除前后空格
*/
function trim(str) {
return str.replace(/(^\s*)|(\s*$)/g, "");
}