zoukankan      html  css  js  c++  java
  • 将long时间格式转化为标准日期格式

    标准时间格式和long格式的转换

    1、将long时间格式转化为几秒前

    /* 时间 */
    function getDateDiff(hours){
        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 - hours;
        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;
        if(monthC>=1){
            result="" + parseInt(monthC) + "月前";
        }
        else if(weekC>=1){
            result="" + parseInt(weekC) + "周前";
        }
        else if(dayC>=1){
            result=""+ parseInt(dayC) +"天前";
        }
        else if(hourC>=1){
            result=""+ parseInt(hourC) +"小时前";
        }
        else if(minC>=1){
            result=""+ parseInt(minC) +"分钟前";
        }else
            result="刚刚";
        return result;
    }
    function getDateTimeStamp(dateStr){
        return Date.parse(dateStr.replace(/-/gi,"/"));
    }
    
    //获取当前时间
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
        return currentdate;
    }
    
    
    //调用
    var hours = JSON.stringify(data[0].inputtime.time);
    $('#hour').text(getDateDiff(hours));

    2、将long时间格式转化为标准格式

    //将long时间格式转化为标准格式
        function dateFormatUtil(longTypeDate){
            var dateTypeDate = "";
            var date = new Date();
            date.setTime(longTypeDate);
            dateTypeDate += date.getFullYear();   //
            dateTypeDate += "-" + getMonth(date); //
            dateTypeDate += "-" + getDay(date);   //
            return dateTypeDate;
        }
    
        //返回 01-12 的月份值
        function getMonth(date){
            var month = "";
            month = date.getMonth() + 1; //getMonth()得到的月份是0-11
            if(month<10){
                month = "0" + month;
            }
            return month;
        }
    
        //返回01-30的日期
        function getDay(date){
            var day = "";
            day = date.getDate();
            if(day<10){
                day = "0" + day;
            }
            return day;
        }
    
    //调用
    dateFormatUtil(data[0].inputtime.time)
  • 相关阅读:
    CSS练习
    大作业“抽屉页面”html+css
    html练习代码
    协程-基于TCP的高并发通信
    协程-爬虫示例
    互斥锁,递归锁,信号量
    三层架构(我的理解及详细分析)
    递归算法经典实例小结(C#实现)
    使用XmlWriter写Xml
    使用XmlReader读Xml
  • 原文地址:https://www.cnblogs.com/dxt510/p/7759156.html
Copyright © 2011-2022 走看看