日期字符串转换为天数 昨天日期

timeDetia = function(date) {
	// ios日期兼容处理
	if (uni.getSystemInfoSync().platform == "ios") {
		date = date.replace(/-/g, "/")
	}
	var time;
	var d = new Date(date);
	var n = new Date();
	//获取时间戳
	var dd = d.getTime();
	var h = d.getHours();
	var m = d.getMinutes();
	var Y = d.getFullYear();
	var M = d.getMonth() + 1;
	var D = d.getDate();
	//现在时间
	var nn = n.getTime();
	var hh = n.getHours();
	var mm = n.getMinutes();
	var YY = n.getFullYear();
	var MM = n.getMonth() + 1;
	var DD = n.getDate();
	h = getTimes(h)
	m = getTimes(m)
	if (D == DD && M == MM && Y == YY) {
		time = h + ':' + m;

	} else if (D + 1 == DD && M == MM && Y == YY) {
		time = '昨天' + ' ' + h + ':' + m;
	} else {
		M = getTimes(M)
		D = getTimes(D)
		time = Y + '年' + M + '月' + D + '日' + ' ' + h + ':' + m;
	}

	function getTimes(time, h = 10) {
		return time < h ? `0${time}` : time
	}
	return time;
}

你可能感兴趣的:(js)