• js 时间格式化和时间戳


     formatMessageTime: function (time) {
            var week = {
                "0": "星期日",
                "1": "星期一",
                "2": "星期二",
                "3": "星期三",
                "4": "星期四",
                "5": "星期五",
                "6": "星期六"
            };
            var oneDay = 24 * 60 * 60 * 1000,
                oneWeek = 6 * 24 * 60 * 60 * 1000,
                oneYear = 365 * 24 * 60 * 60 * 1000;
            // return function (time) {
            if (!time) {
                return '';
            }
            if (typeof (time) === 'string') {
                return time;
            }
            var sendTime = new Date(time),
                currentTime = new Date();
            var yesterdayTime = new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate());
    
            var minutes = sendTime.getMinutes();
            var hours = sendTime.getHours();
            minutes = minutes > 9 ? minutes : '0' + minutes;
            hours = hours > 9 ? hours : '0' + hours;
    
            var showSendTime = hours + ':' + minutes;
            var showDate;
            // 今天
            if (sendTime >= yesterdayTime) {
                return showSendTime;
            }
            // 昨天
            else if (sendTime >= yesterdayTime - oneDay) {
                showDate = '昨天';
            }
            // 一周内
            else if (sendTime >= yesterdayTime - oneWeek) {
                showDate = week[sendTime.getDay().toString()];
            }
            // 今年内
            else if (sendTime.getFullYear() == currentTime.getFullYear()) {
                showDate = (sendTime.getMonth() + 1) + '/' + sendTime.getDate();
            }
    
            // 本世纪
            else if (sendTime.getFullYear().toString().slice(0, 2) == currentTime.getFullYear().toString().slice(0, 2)) {
                showDate = sendTime.getFullYear().toString().slice(2) + '/' + (sendTime.getMonth() + 1) + '/' + sendTime.getDate();
            }
            //
            else {
                showDate = sendTime.getFullYear() + '/' + (sendTime.getMonth() + 1) + '/' + sendTime.getDate();
            }
            return showDate + ' ' + showSendTime;
            // };
        },
        formatTime: function (time) {
            var d = new Date(time);
            var year = d.getFullYear();
            var month = d.getMonth() + 1;
            var day = d.getDate() < 10 ? '0' + d.getDate() : '' + d.getDate();
            var hour = d.getHours();
            var minutes = d.getMinutes();
            var seconds = d.getSeconds();
            // return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds;
            return hour + ':' + minutes;
        },
        formatYMDhmsTime: function (time) {
            var d = new Date(time);
            var year = d.getFullYear();
            var month = d.getMonth() + 1;
            var day = d.getDate() < 10 ? '0' + d.getDate() : '' + d.getDate();
            var hour = d.getHours();
            var minutes = d.getMinutes();
            var seconds = d.getSeconds();
            return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds;
            // return hour + ':' + minutes;
        },
        formatYMDhmTime: function (time) {
            var d = new Date(time);
            var year = d.getFullYear();
            var month = d.getMonth() + 1;
            var day = d.getDate() < 10 ? '0' + d.getDate() : '' + d.getDate();
            var hour = d.getHours();
            var minutes = d.getMinutes() <10 ? '0' + d.getMinutes() : '' + d.getMinutes();
            var seconds = d.getSeconds();
            return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes;
            // return hour + ':' + minutes;
        },
    
  • 相关阅读:
    kaggle比赛房价预测
    20192421曾禹涵汇编语言程序设计学习笔记
    Quarkus初体验:动态加载和原生部署
    Windows Android 子系统(WSA)安装
    C#笔记之又谈装箱与拆箱(boxing and unboxing)
    vue前端安全问题学习
    AOP源码解析
    【C#】关于bool?和bool
    【Win8】动画&异步编程
    【WindowsPhone】(二)主题、样式与模版
  • 原文地址:https://www.cnblogs.com/sxz2008/p/7511358.html
Copyright © 2020-2023  润新知