【原创】Js:日期处理(日期格式必须【yyyy-mm-dd】才能转成long的毫秒!其他的不是【年-月-日】的格式,结果会是【NaN】)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test date</title>
</head>
<body>

    <script type="text/javascript">
    
        document.write("三种获取时间戳的方式:");
        document.write("<br/>");
        var myDate = new Date();
        document.write("系统时间:" + myDate);
        document.write("<br/>");
        document.write("<br/>");
        // 获取时间戳:第一种方式:
        var timestamp1 = Date.parse(myDate);
        document.write("第一种方式:Date.parse(new Date())获取方式时间戳:" + timestamp1);
        
        document.write("<br/>")
        var timestamp2 = myDate.valueOf();
        
        document.write("第二种方式:new Date().valueOf()获取方式时间戳:" + timestamp2);
        
        var timestamp3 = myDate.getTime();
        document.write("<br/>")
        document.write("第三种方式:new Date().getTime()获取方式时间戳:" + timestamp3);
        
        document.write("<br/><br/><br/>");
        
        document.write("<font color='red'>日期格式必须【yyyy-mm-dd】才能转成long的毫秒!其他的不是【年-月-日】的格式,结果会是【NaN】</font>");
        
        document.write("<br/><br/><br/>");
        document.write("日期类型转换成long类型");
        var strTime = "2015/04/12";  //字符串日期格式
        var strTemp = strTime.replace(/-/g, "/");
        document.write("strTemp: "+strTemp);
        var date1 = new Date(strTemp);
        
        document.write("<br/>");
        document.write("2015-04-12转换成Date: " + date1);
        document.write("<br/>");
        document.write(date1.getTime());
        
        // =====================正则表达式=========
        /**            
        *        /-/g是js的正则表达式,匹配划线的  
        *        re   =   /pattern/[flags]  
        *        用   "/ "   字符分隔模式    
        *        g   (全文查找出现的所有   pattern)    
        *        i   (忽略大小写)    
        *        m   (多行查找) 
        */ 
        // ===================================
        
        document.write("<br/><br/><br/>");
        
        
        
        //=======================基础方法========
        
        var myDate = new Date();
        myDate.getYear(); //获取当前年份(2位)
        myDate.getFullYear(); //获取完整的年份(4位,1970-????)
        myDate.getMonth(); //获取当前月份(0-11,0代表1月)
        myDate.getDate(); //获取当前日(1-31)
        myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
        myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
        myDate.getHours(); //获取当前小时数(0-23)
        myDate.getMinutes(); //获取当前分钟数(0-59)
        myDate.getSeconds(); //获取当前秒数(0-59)
        myDate.getMilliseconds(); //获取当前毫秒数(0-999)
        myDate.toLocaleDateString(); //获取当前日期
        var mytime = myDate.toLocaleTimeString(); //获取当前时间
        myDate.toLocaleString(); //获取日期与时间
        
        function getLongByDate(strDate)
        {
            var result = null;
            if (strDate == null || strDate == "" || strDate == undefined)
            {
                return result;
            }
            else
            {
                // 如果传入的日期字符串格式不正确,返回空字符串
                try 
                {
                    var resultDate = new Date(strDate.replace(/-/g, "/"));
                    result = resultDate.getTime();
                }
                catch (ex)
                {
                    //alert("日期格式不正确");
                }
            }
            
            return result;
        }
        
        document.write("<br/><br/>");
        document.write("getLongByDate(str): " + getLongByDate("15-04-2015"));
        document.write("<br/><br/>");
        
        document.write("aaaa: " + new Date(('2014/05/10 13:25:50').replace(new RegExp("-","gm"),"/")).getTime());
    </script>
    <script>
    document.write("long类型转换成日期类型");
    Date.prototype.format=function(fmt) {
        var o = {
                "M+" : this.getMonth() + 1,                                     //月份        
                "d+" : this.getDate(),                                             //
                "h+" : this.getHours() % 12 == 0 ? 12 : this.getHours() % 12,     //12小时制        
                "H+" : this.getHours(),                                         //24小时制        
                "m+" : this.getMinutes(),                                         //
                "s+" : this.getSeconds(),                                         //
                "q+" : Math.floor((this.getMonth() + 3) / 3),                     //季度        
                "S" : this.getMilliseconds()                                    //毫秒        
            };
        var week = {
            "0" : "u65e5",
            "1" : "u4e00",
            "2" : "u4e8c",
            "3" : "u4e09",
            "4" : "u56db",
            "5" : "u4e94",
            "6" : "u516d"
        };
        if(/(y+)/.test(fmt)){
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));        
        }
        if(/(E+)/.test(fmt)){
            fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "u661fu671f" : "u5468") : "")+week[this.getDay()+""]);        
        }
        for(var k in o){
            if(new RegExp("("+ k +")").test(fmt)){        
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));        
            }
        }       
        return fmt;
    }


    var t = 1428138194801;
    var d = new Date();
    d.setTime(t);
    var s=d.format('yyyy-MM-dd HH:mm:ss');
    document.write("long类型转换成日期类型: " + s);
    
    
    document.write("<br/><br/>");
    document.write("日期类型转换成long类型");
    var strTime = "2015-04-15";  //字符串日期格式
    var date1 = new Date(strTime.replace(/-/g, "/"));
    document.write("<br/>");
    document.write("2015-04-15转换成Date: " + date1);
    document.write("<br/>");
    document.write(date1.getTime());
    
    function getLongByDate(strDate)
    {
        var result = null;
        if (strDate == null || strDate == "" || strDate == undefined)
        {
            return result;
        }
        else
        {
            // 如果传入的日期字符串格式不正确,返回空字符串
            try 
            {
                var resultDate = new Date(strDate.replace(/-/g, "/"));
                result = resultDate.getTime();
            }
            catch (ex)
            {
                //alert("日期格式不正确");
            }
        }
        
        return result;
    }
    
    document.write("<br/><br/>");
    document.write("getLongByDate(str): " + getLongByDate("15-04-2015"));
    
    </script>

</body>
</html>
原文地址:https://www.cnblogs.com/gmq-sh/p/4392682.html