zoukankan      html  css  js  c++  java
  • 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换

        var dateDiff = function (timestamp) {
            // 补全为13位
            var arrTimestamp = (timestamp + '').split('');
            for (var start = 0; start < 13; start++) {
                if (!arrTimestamp[start]) {
                    arrTimestamp[start] = '0';
                }
            }
            timestamp = arrTimestamp.join('') * 1;
            var minute = 1000 * 60;
            var hour = minute * 60;
            var day = hour * 24;
            var halfamonth = day * 15;
            var month = day * 30;
            var now = new Date().getTime();
            var diffValue = now - timestamp;
    
            // 如果本地时间反而小于变量时间
            if (diffValue < 0) {
                return '不久前';
            }
            // 计算差异时间的量级
            var monthC = diffValue / month;
            var weekC = diffValue / (7 * day);
            var dayC = diffValue / day;
            var hourC = diffValue / hour;
            var minC = diffValue / minute;
    
            // 数值补0方法
            var zero = function (value) {
                if (value < 10) {
                    return '0' + value;
                }
                return value;
            };
    
            // 使用
            if (monthC > 4) {
                // 超过1年,直接显示年月日
                return (function () {
                    var date = new Date(timestamp);
                    return date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
                })();
            } else if (monthC >= 1) {
                return parseInt(monthC) + "月前";
            } else if (weekC >= 1) {
                return parseInt(weekC) + "周前";
            } else if (dayC >= 1) {
                return parseInt(dayC) + "天前";
            } else if (hourC >= 1) {
                return parseInt(hourC) + "小时前";
            } else if (minC >= 1) {
                return parseInt(minC) + "分钟前";
            }
            return '刚刚';
        };
        console.log(dateDiff(1560387311));  // 2014年09月19日
        console.log(dateDiff(1560400790));  // 9月前
        console.log(dateDiff(1550062750));  // 2月前
        console.log(dateDiff(1547384350));  // 3周前
        console.log(dateDiff(1505283100802));  // 1分钟前

    JS时间格式化,时间戳的转换

    //时间转时间戳
    //将Thu Sep 20 2018 16:47:52 GMT+0800 (中国标准时间)转换为1537433272051
    
    console.log(Date.parse(new Date()))
    console.log(new Date().getTime())
    

    //将"2018-09-20 16:50:48"转换为1537433448000 var timeDate = "2018-09-20 16:50:48"; var Time = new Date(timeDate); var timestemp = Time.getTime(); console.log(timestemp)
    • Date.parse() :时间字符串可以直接Date.parse(datestring),不需要 new Date()
    • Date.getTime() :需要将时间字符串先new Date(),再使用Date.getTime()
  • 相关阅读:
    log4net Appenders
    cnblogs 安家了
    log4net 资源索引贴
    Log2Console A Generic Log Viewer (for Log4Net, NLog...)
    [前端技术]如何加深对JavaScipt中的Math.ceil() 、Math.floor() 、Math.round() 三个函数的理解
    msiexec 命令使用文档
    “安装和部署”文章索引
    一句SQL实现获取自增列操作
    MsChart 部署遇到的一点问题
    [Asp.net]ZipHelper 在线压缩解压帮助类(SharpZipLib组件实现)
  • 原文地址:https://www.cnblogs.com/li-sir/p/11019473.html
Copyright © 2011-2022 走看看