• 时间戳显示为多少分钟前,多少天前的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()
  • 相关阅读:
    unity3d 随机生成地形之随机山脉
    unity3d ppsspp模拟器中的post processing shader在unity中使用
    unity3d shader之实时室外光线散射(大气散射)渲染
    Unity3d 游戏中的实时降噪-对Square Enix文档的研究与实现
    Unity3d 获取屏幕depth与normal
    unity3d Hair real time rendering 真实头发实时渲染
    java.net.URL类
    Springboot定时任务
    Base64编码
    ShiroUtil 对密码进行加密
  • 原文地址:https://www.cnblogs.com/li-sir/p/11019473.html
Copyright © 2020-2023  润新知