JS中Unix时间戳转换日期格式

版权声明:本文为博主原创文章,未经博主允许不得转载,如文章对您有帮助,请页面左侧随意打赏。 https://blog.csdn.net/smartsmile2012/article/details/73087810
<!doctype html>
<html>
<head>
    <title>Unix时间戳转换成日期格式</title>
    <script>
        var a = FormatDateTime("/Date(1497232433000)/")
        var b = FormatDateTime("1497232740000")
        console.log(a); //2017-06-12 09:53:53
        console.log(b); //2017-06-12 09:59:00
        alert(a);
        alert(b);
        /*
		* Unix时间戳转换成日期格式  FormatDateTime("1497232433000")
		* @param UnixTime Unix时间戳
		* @return yyyy-MM-dd HH:mm:ss
		*/
        function FormatDateTime(UnixTime) {
            var a = UnixTime.replace("/Date(", "").replace(")/", "");
            var date = new Date(parseInt(a));
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? ('0' + m) : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            var h = date.getHours();
            h = h < 10 ? ('0' + h) : h;
            var minute = date.getMinutes();
            var second = date.getSeconds();
            minute = minute < 10 ? ('0' + minute) : minute;
            second = second < 10 ? ('0' + second) : second;
            return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
        };
    </script>
</head>
<body>
</body>
</html>

展开阅读全文

没有更多推荐了,返回首页