louby
 

接收json数据,日期格式为:"\/Date(1414078309687)\/"

var value = "/Date(1414078309687)/";

 

var da = eval(\'new \' + value.replace(\'/\', \'\', \'g\'));
da.toLocaleDateString()  //2014/10/23
da.toLocaleTimeString()  //下午11:31:49

//通过正则拿到里面数字。
value.replace(/\/Date\((\d+)\)\//gi, \'$1\');   //g 全局 i不区分大小写

var date = new Date();
date.setTime("1414078309687");  //value通过截取字符串只取数字。
date.toLocaleDateString()  //2014/10/23
date.toLocaleTimeString()  //下午11:31:49


//json时间格式化 function formatDate(value) { if (value == null || value == "") { return null; } var date = new Date(); date.setTime(value.replace(/\/Date\((\d+)\)\//gi, \'$1\')); //value通过截取字符串只取数字。 //2014/10/23 //return date.toLocaleString().replace(\'/\', \'-\').replace(\'/\', \'-\'); return format(date, "yyyy-MM-dd HH:mm:ss"); }

  

//时间格式转换
        function format(date, fmt) {
            var o = {
                "M+": date.getMonth() + 1, //月份
                "d+": date.getDate(), //日
                "H+": date.getHours(), //小时
                "m+": date.getMinutes(), //分
                "s+": date.getSeconds(), //秒
                "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            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; }

  

分类:

技术点:

相关文章: