IM用到的时间方法timeUtil

时间格式化和时间处理,平时用不到,但总有用的到的时候,用的时候去查询就很花时间,还要实验对不对,符不符合自己的诉求,所以记录了下时间会用到的方法;

时间格式化
export function ParseTime (time: number, cFormat: string): string {
    if (arguments.length === 0 || !time) {
      return '';
    };
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
    let date: Date;
    if (typeof time === 'object') {
      date = time;
    } else {
      if (('' + time).length === 10) {
        time = time * 1000;
      };
      date = new Date(time);
    };
    const formatObj: any = {
      y: date.getFullYear(),
      m: date.getMonth() + 1,
      d: date.getDate(),
      h: date.getHours(),
      i: date.getMinutes(),
      s: date.getSeconds(),
      a: date.getDay()
    };
    const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
      let value: any = formatObj[key];
      if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] };
      if (result.length > 0 && value < 10) {
        value = '0' + value
      };
      return value || 0;
    });
    return timeStr;
};


获取当前时间往前推几个月
export function handleForwardMonth(month: number) {
  var today = new Date() //当天
  today.setMonth(today.getMonth()-month)//三个月前,时间戳
  today.toLocaleDateString()//格式化时间
  return new Date(today).getTime();
}
一些常用api
// 获取当月第一天
new Date().setDate(1)
// 获取这个月多少天
new Date().getDate()
// 获取指定月第一天和最后一天
new Date(2022, 1, 1).getDate() // 第一天
new Date(2022, 1, 0).getDate() // 最后一天
其它拓展
/* 聊天历史会话,通常会展示最后一条消息的时间 */
export function HandleChatListTime(currentTimer: number, isBackTime = true) {
  if (!currentTimer) { return; }
  if (isToday(currentTimer)) {
      // 是否今天
      return '今天 ' + (isBackTime? ParseTime(currentTimer, '{h}:{i}') : '');
  } else if (isYesterday(currentTimer)) {
      // 是否昨天
      return '昨天 ' + (isBackTime ? ParseTime(currentTimer, '{h}:{i}') : '');
  } else if (isThatYear(currentTimer)) {
      // 是否今年
      return ParseTime(currentTimer, '{m}月{d}日');
  } else {
      return ParseTime(currentTimer, '{y}年{m}月{d}日');
  };
};
// 是否今年
export function isThatYear(time: number) {
    const date = new Date(time).getFullYear();
    const cur = new Date().getFullYear();
    return date === cur;
};
// 是否昨天
export function isYesterday(time: number) {
    const date = new Date();
    const today = handleYearMothDay(date);
    const todayTime = new Date(today).getTime(); // 当前凌晨的时间
    // 昨天凌晨的世界
    const yesterdayTime = new Date(todayTime - 24 * 60 * 60 * 1000).getTime();
    return time < todayTime && yesterdayTime <= time;
};
// 是否今天
export function isToday(time: number) {
    let data = handleYearMothDay(new Date(time));
    let cur = handleYearMothDay(new Date());
    return data === cur;
};
// 返回: `${year}/${month}/${day}`
export function handleYearMothDay(date: any) {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const today = `${year}/${month}/${day}`;
    return today;
};
/* 处理聊天时间_end */
/* 处理倒计时; 比方说考试倒计时, 或者会议倒计时 */
export function handleCountDown(startTime: number, endTime: number) {
  var dStr = '', hStr = '', mStr = '', sStr = '';
  //把剩余时间毫秒数转化为秒
  var times: any = (endTime - startTime) / 1000;
  var d = Math.floor(times / 86400);
  dStr = d < 10 ? "0" + d : d + '';
  //计算小时数 转化为整数
  var h = parseInt((times / 60 / 60 % 24) + '');
  //如果小时数小于 10,要变成 0 + 数字的形式 赋值给盒子
  hStr = h < 10 ? "0" + h : h + '';
  //计算分钟数 转化为整数
  var m = parseInt((times / 60 % 60) + '');
  //如果分钟数小于 10,要变成 0 + 数字的形式 赋值给盒子
  mStr = m < 10 ? "0" + m : m + '';
  //计算描述 转化为整数
  var s = parseInt((times % 60) + '');
  sStr = s < 10 ? "0" + s : s + '';
  // dStr.length > 0 ? `${dStr}天${hStr}:${mStr}:${sStr}` : `${hStr}:${mStr}:${sStr}`
  return `${hStr}:${mStr}:${sStr}`;
};
// 视频通话后,有消息会显示通话时长
export function HandleTalkTime(startTime: number, endTime: number) {
  var dStr = '', hStr = '', mStr = '', sStr = '';
  //把剩余时间毫秒数转化为秒
  var times: any = (endTime - startTime) / 1000;
  var d = Math.floor(times / 86400);
  dStr = d < 10 ? "0" + d : d + '';
  //计算小时数 转化为整数
  var h = parseInt((times / 60 / 60 % 24) + '');
  //如果小时数小于 10,要变成 0 + 数字的形式 赋值给盒子
  hStr = h < 10 ? "0" + h : h + '';
  //计算分钟数 转化为整数
  var m = parseInt((times / 60 % 60) + '');
  //如果分钟数小于 10,要变成 0 + 数字的形式 赋值给盒子
  mStr = m < 10 ? "0" + m : m + '';
  //计算描述 转化为整数
  var s = parseInt((times % 60) + '');
  sStr = s < 10 ? "0" + s : s + '';
  if (dStr !== '00') {
    return `${dStr}${hStr}${mStr}${sStr}`;
  } else if (hStr !== '00') {
    return `${hStr}${mStr}${sStr}`;
  } else if (mStr !== '00') {
    return `${mStr}${sStr}`;
  } else if (sStr !== '00') {
    return `${sStr}`;
  } else {
    return '0秒';
  };
}
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值