zoukankan      html  css  js  c++  java
  • 时间格式化

    常用时间格式化用法

    var Time = {
        // 获取当前时间戳
        getUnix: function () {
            var date = new Date();
            return date.getTime();
        },
        // 获取今天0点0分0秒的时间戳
        getTodayUnix: function () {
            var date = new Date();
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
            date.setMilliseconds(0);
            return date.getTime();
        },
        // 获取今年1月1日0点0分0秒的时间戳
        getYearUnix: function () {
            var date = new Date();
            date.setMonth(0);
            date.setDate(1);
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
            date.setMilliseconds(0);
            return date.getTime();
        },
        // 获取标准年月日
        getLastDate: function(time) {
            var date = new Date(time);
            var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
            var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
            return date.getFullYear() + '-' + month + "-" + day;
        },
        // 转换时间
        getFormatTime: function(timestamp) {
            var now = this.getUnix();    //当前时间戳
            var today = this.getTodayUnix(); //今天0点时间戳
            var year = this.getYearUnix();   //今年0点时间戳
            var timer = (now - timestamp) / 1000;   // 转换为秒级时间戳
            var tip = '';
    
            if (timer <= 0) {
                tip = '刚刚';
            } else if (Math.floor(timer/60) <= 0) {
                tip = '刚刚';
            } else if (timer < 3600) {
                tip = Math.floor(timer/60) + '分钟前';
            } else if (timer >= 3600 && (timestamp - today >= 0) ) {
                tip = Math.floor(timer/3600) + '小时前';
            } else if (timer/86400 <= 31) {
                tip = Math.ceil(timer/86400) + '天前';
            } else {
                tip = this.getLastDate(timestamp);
            }
            return tip;
        }
    };

    // 获取今天时间戳
    getTodayTime = function () {
        const date = new Date();
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        date.setMilliseconds(0);
        return date.getTime();
    };
    
    // 获取上一天日期
    prevDay = function (timestamp = (new Date()).getTime()) {
        const date = new Date(timestamp);
        const year = date.getFullYear();
        const month = date.getMonth() + 1 < 10
            ? '0' + (date.getMonth() + 1)
            : date.getMonth() + 1;
        const day = date.getDate() < 10
            ? '0' + date.getDate()
            : date.getDate();
        return year + '' + month + '' + day;
    };
    function ago(time: Date) {
      const date3 = new Date().getTime() - new Date(time).getTime();
      const days = Math.floor(date3 / (24 * 3600 * 1000));
    
      // 计算出小时数
      const leave1 = date3 % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
      const hours = Math.floor(leave1 / (3600 * 1000));
      // 计算相差分钟数
      const leave2 = leave1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
      const minutes = Math.floor(leave2 / (60 * 1000));
      // 计算相差秒数
      const leave3 = leave2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
      const seconds = Math.round(leave3 / 1000);
      // alert(" 相差 " + days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒")
      if (days !== 0) {
        if (days > 365) {
          return `${Math.floor(days / 365)}年前`;
        } else if (days > 30) {
          return `${Math.floor(days / 30)}月前`;
        } else if (days < 0) {
          return "刚刚";
        }
    
        return `${days}天前`;
      } else if (hours !== 0) {
        return `${hours}小时前`;
      } else if (minutes !== 0) {
        return `${minutes}分钟前`;
      } else if (seconds !== 0) {
        return `${seconds}秒前`;
      } else if (seconds === 0) {
        return "刚刚";
      }
    }
  • 相关阅读:
    (转)IntelliJ IDEA 插件 阿里巴巴Java开发手册(Alibaba Java Coding Guidelines)
    idea快捷键整理
    (转)mysql使用Navicat 导出和导入数据库
    (转)Intellij Idea工具栏添加打开选中文件的资源管理器位置
    Intellij IDEA设置类注释和方法注释
    mavn jar包依赖冲突解决
    我的Keras使用总结(3)——利用bottleneck features进行微调预训练模型VGG16
    我的Keras使用总结(2)——构建图像分类模型(针对小数据集)
    我的Keras使用总结(1)——Keras概述与常见问题整理
    数据竞赛实战(5)——方圆之外
  • 原文地址:https://www.cnblogs.com/zzxuan/p/10099858.html
Copyright © 2011-2022 走看看