ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

关于时间的那些事

2022-05-14 01:34:47  阅读:169  来源: 互联网

标签:return 时间 那些 Date dateObj 关于 new shorttime let


export default{
// 计算当前日期星座
getHoroscope(date) {
let c = ['摩羯','水瓶','双鱼','白羊','金牛','双子','巨蟹','狮子','处女','天秤','天蝎','射手','摩羯']
date=new Date(date);
let month = date.getMonth() + 1;
let day = date.getDate();
let startMonth = month - (day - 14 < '865778999988'.charAt(month));
return c[startMonth]+'座';
},
// 计算指定时间与当前的时间差
sumAge(data){
let dateBegin = new Date(data.replace(/-/g, "/"));
let dateEnd = new Date();//获取当前时间
let dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数
let dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));//计算出相差天数
let leave1=dateDiff%(24*3600*1000) //计算天数后剩余的毫秒数
let hours=Math.floor(leave1/(3600*1000))//计算出小时数
//计算相差分钟数
let leave2=leave1%(3600*1000) //计算小时数后剩余的毫秒数
let minutes=Math.floor(leave2/(60*1000))//计算相差分钟数
//计算相差秒数
let leave3=leave2%(60*1000) //计算分钟数后剩余的毫秒数
let seconds=Math.round(leave3/1000)
return dayDiff+"天 "+hours+"小时 ";
},
// 获取聊天时间(相差300s内的信息不会显示时间)
getChatTime(v1,v2){
v1=v1.toString().length<13 ? v1*1000 : v1;
v2=v2.toString().length<13 ? v2*1000 : v2;
if(((parseInt(v1)-parseInt(v2))/1000) > 300){
return this.gettime(v1);
}
},
// 人性化时间格式
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}:{ss}");
return this.dateFormat(new Date(shorttime),"{t}:{ii}:{ss}");
} else if(cha < 518400){
// 隔天 显示日期+时间
// return this.dateFormat(new Date(shorttime),"{Mon}月{DD}日 {A} {t}:{ii}:{ss}");
return this.dateFormat(new Date(shorttime),"{Mon}月{DD}日 {t}:{ii}:{ss}");
} else {
// 隔年 显示完整日期+时间
// return this.dateFormat(new Date(shorttime),"{Y}-{MM}-{DD} {A} {t}:{ii}:{ss}");
return this.dateFormat(new Date(shorttime),"{Y}-{MM}-{DD} {t}:{ii}:{ss}");
}
},
gettime1(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),"{t}:{ii}");
} else if(cha < 518400){
// 隔天 显示日期+时间
return this.dateFormat(new Date(shorttime),"{Mon}月{DD}日");
} else {
// 隔年 显示完整日期+时间
return this.dateFormat(new Date(shorttime),"{Y}-{MM}-{DD}");
}
},
parseNumber(num) {
return num < 10 ? "0" + num : num;
},

dateFormat(date, formatStr) {
let dateObj = {},
rStr = /\{([^}]+)\}/,
// mons = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'];
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["t"] = 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;
}
}

标签:return,时间,那些,Date,dateObj,关于,new,shorttime,let
来源: https://www.cnblogs.com/yhxy/p/16268908.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有