zoukankan      html  css  js  c++  java
  • js时间转换相关

    1.json时间格式转换

    function ChangeDateFormat(jsondate) {
        if (!jsondate||jsondate.length < 1) {return ""; }
        jsondate = jsondate.replace("/Date(", "").replace(")/", "");
        if (jsondate.indexOf("+") > 0) {
            jsondate = jsondate.substring(0, jsondate.indexOf("+"));
        }
        else if (jsondate.indexOf("-") > 0) {
            jsondate = jsondate.substring(0, jsondate.indexOf("-"));
        }
        
        var date = new Date(parseInt(jsondate, 10));
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    }
    
    //带日期
    function ChangeDateTimeFormat(jsondate) {
        if (!jsondate || jsondate.length < 1) { return ""; }
        jsondate = jsondate.replace("/Date(", "").replace(")/", "");
        if (jsondate.indexOf("+") > 0) {
            jsondate = jsondate.substring(0, jsondate.indexOf("+"));
        }
        else if (jsondate.indexOf("-") > 0) {
            jsondate = jsondate.substring(0, jsondate.indexOf("-"));
        }
    
        var date = new Date(parseInt(jsondate));
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
    }

     2.js中两个时间的差

    function DateS(a,b,c) {
        var arr = ChangeDateFormat(a).split("-");
        var starttime = new Date(arr[0], arr[1] - 1, arr[2]);
        var starttimes = starttime.getTime(); 
    
        var arr = ChangeDateFormat(b).split("-");
        var lktimes = new Date(arr[0], arr[1] - 1, arr[2]);
        var lktimes = starttime.getTime();
        var ltime = 0;
        if (c === 'y' || c === 'Y') {
              ltime = (starttimes - lktimes) / (86400000*365);
        }
    
        if (c === 'm' || c === 'M') {
              ltime = (starttimes - lktimes) / (86400000 * 365/12);
        } 
        if (c === 'd' || c === 'D') {
            ltime = (starttimes - lktimes) / 86400000;
        }
        if (c === 'h' || c === 'H') {
            ltime = (starttimes - lktimes) / 3600000;
        }
        if (c === 'MM' || c === 'mm') {
            ltime = (starttimes - lktimes) / 60000;
        }
        if (c === 'ss' || c === 'SS') {
            ltime = (starttimes - lktimes) / 1000;
        }
        return ltime;
    }
    //输入的值与当前时间差
    function DateSNow(a,  c) {
        var arr = ChangeDateFormat(a).split("-");
        var starttime = new Date(arr[0], arr[1] - 1, arr[2]);
       
        var starttimes = starttime.getTime();
    
        var myDate = new Date();
       
        var mmDate = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate());
        
        var lktimes = mmDate.getTime(); 
        var ltime = 0;
        if (c === 'y' || c === 'Y') {
            ltime = (starttimes - lktimes) / (86400000 * 365);
        }
    
        if (c === 'm' || c === 'M') {
            ltime = (starttimes - lktimes) / (86400000 * 365 / 12);
        }
        if (c === 'd' || c === 'D') {
            ltime = (starttimes - lktimes) / 86400000;
        }
        if (c === 'h' || c === 'H') {
            ltime = (starttimes - lktimes) / 3600000;
        }
        if (c === 'MM' || c === 'mm') {
            ltime = (starttimes - lktimes) / 60000;
        }
        if (c === 'ss' || c === 'SS') {
            ltime = (starttimes - lktimes) / 1000;
        }
        return ltime;
    }
  • 相关阅读:
    第六十篇、音视频采集硬编码(H264+ACC)
    第十三篇、Swift_Nav自定义返回按钮后或者隐藏导航栏,Pop返回手势失效的解决方法 Pop全局返回添加的方法
    第五十九篇、OC录制小视频
    第五十八篇、iOS 微信聊天发送小视频的秘密
    第五十七篇、AVAssetReader和AVAssetWrite 对视频进行编码
    第五十六篇、OC打开本地和网络上的word、ppt、excel、text等文件
    Objective-C 编码建议
    在block中使用self
    纯代码TableView自适应高度(很老的使用方法)
    iOS应用架构谈 网络层设计方案
  • 原文地址:https://www.cnblogs.com/lecone/p/4583813.html
Copyright © 2011-2022 走看看