人性化时间格式

人性化时间格式
gettime(shorttime){
shorttime=shorttime.toString().length<13 ? shorttime*1000 : shorttime;
let now = (new Date()).getTime();
let cha = (now-parseInt(shorttime))/1000;

	if (cha < 43200) {
		// 当天
		return this.dateFormat(new Date(shorttime),"{A} {t}:{ii}");
	} else if(cha < 518400){
		// 隔天 显示日期+时间
		return this.dateFormat(new Date(shorttime),"{Mon}月{DD}日 {A} {t}:{ii}");
	} else {
		// 隔年 显示完整日期+时间
		return this.dateFormat(new Date(shorttime),"{Y}-{MM}-{DD} {A} {t}:{ii}");
	}
},

parseNumber(num) {
	return num < 10 ? "0" + num : num;
},
 
dateFormat(date, formatStr) {
	let dateObj = {},
		rStr = /\{([^}]+)\}/,
		mons = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
	 
	dateObj["Y"] = date.getFullYear();
	dateObj["M"] = date.getMonth() + 1;
	dateObj["MM"] = this.parseNumber(dateObj["M"]);
	dateObj["Mon"] = mons[dateObj['M'] - 1];
	dateObj["D"] = date.getDate();
	dateObj["DD"] = this.parseNumber(dateObj["D"]);
	dateObj["h"] = date.getHours();
	dateObj["hh"] = this.parseNumber(dateObj["h"]);
	dateObj["t"] = dateObj["h"] > 12 ? dateObj["h"] - 12 : dateObj["h"];
	dateObj["tt"] = this.parseNumber(dateObj["t"]);
	dateObj["A"] = dateObj["h"] > 12 ? '下午' : '上午';
	dateObj["i"] = date.getMinutes();
	dateObj["ii"] = this.parseNumber(dateObj["i"]);
	dateObj["s"] = date.getSeconds();
	dateObj["ss"] = this.parseNumber(dateObj["s"]);
 
	while(rStr.test(formatStr)) {
		formatStr = formatStr.replace(rStr, dateObj[RegExp.$1]);
	}
	return formatStr;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值