UNIX时间戳/日期转换

时间:2021-03-23 02:34:02

需求做了,QA没时间测,偷个懒写了这个东东,网上有很多,本来想弄百度开发者,但是不太懂怎么弄,所以,你懂的

<!DOCTYPE html>
<html>
<head>
    <title>UNIX时间戳/日期转换</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="http://cdn.bootcss.com/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script type="text/javascript">
        //时间戳转日期时间
        function getDate() {
            var UnixTime = $('#UnixTime').val() * 1000;
            var dateObj = new Date(UnixTime);
            var UnixTimeToDate = dateObj.getUTCFullYear() + ' 年 ' + (dateObj.getUTCMonth() +1 ) + ' 月 ' + dateObj.getUTCDate() + ' 日 ' + dateObj.getUTCHours() + ':' + dateObj.getUTCMinutes() + ':' + dateObj.getUTCSeconds();
            $('#UnixTimeToDate').text(UnixTimeToDate);
        }

        //日期时间转时间戳
        function getUnixTime() {
            var year = parseInt($('#year').val());
            var month = parseInt($('#month').val());
            var day = parseInt($('#day').val());
            var hour = parseInt($('#hour').val());
            var minute = parseInt($('#minute').val());
            var second = parseInt($('#second').val());
            var now = new Date(new Date().getTime() + 28800000);//东8区时间偏移量为28800000毫秒
            if(isNaN(year)) { year = now.getUTCFullYear(); }
            if(isNaN(month)) { month = now.getUTCMonth(); } else { month--; }
            if(isNaN(day)) { day = now.getUTCDate(); }
            if(isNaN(hour)) { hour = now.getUTCHours(); }
            if(isNaN(minute)) { minute = now.getUTCMinutes(); }
            if(isNaN(second)) { second = now.getUTCSeconds(); }
            var UnixTime = new Date(Date.UTC(year,month,day,hour,minute,second));
            $('#DateToUnixTime').text(UnixTime/1000);
            $('#year').val(year);
            $('#month').val(month+1);
            $('#day').val(day);
            $('#hour').val(hour);
            $('#minute').val(minute);
            $('#second').val(second);
        }
        
        //当前时间
        setInterval("clock()",50);
        function clock() {
            var UnixTime = new Date().getTime();
            var now = new Date(UnixTime+28800000);
            $('#now').text(now.getUTCFullYear() + ' 年 ' + now.getUTCMonth() + ' 月 ' + now.getUTCDate() + ' 日 ' + now.getUTCHours() + ':' + now.getUTCMinutes() + ':' + now.getUTCSeconds());
            $('#nowUnix').text(parseInt(UnixTime/1000));
        }

        //复制当前时间戳
        function copyUnixTime() {
            $('#UnixTime').val($('#nowUnix').text());
        }
    </script>
    <style type='text/css'>
        body { background-color: #CCC; }
        #content { background-color: #FFF; border-radius: 5px; }
        #content .main { padding: 20px; }
        #content .sidebar { padding: 10px; }
        #content p { line-height: 30px; }
    </style>
</head>
  
<body>
  <div class='container'>
    <h1>UNIX时间戳/日期转换</h1>
    <div id='content' class='row-fluid'>
      <div class='main'>
        <p>
            当前时间<span id="now" style="width:200px;display: inline-block;background: #ABCDEF;"></span>
            时间戳<span id="nowUnix" style="width:200px;display: inline-block;background: #ABCDEF;"></span><a href="javascrip:;" class="btn" onclick="copyUnixTime()">复制时间戳到输入框</a>
        </p>
        <p>
            <label style="width:100px;" for="UnixTime">UNIX时间戳</label><input type="text" id="UnixTime" />
            <input type="button" value="转换" class="btn btn-navbar" onclick="getDate()"/>
            格式化后结果<span id="UnixTimeToDate" style="width:200px;display: inline-block;background: #ABCDEF;"> </span>
        <p> 

        <p>
            <input type="text" id="year" style="width:60px;" />
            <label style="width:1em;" for="year">年</label>
            <input type="text" id="month" style="width:60px;" />
            <label style="width:1em;" for="month">月</label>
            <input type="text" id="day" style="width:60px;" />
            <label style="width:1em;" for="day">日</label>
            <input type="text" id="hour" style="width:60px;" />
            <label style="width:1em;" for="hour">时</label>
            <input type="text" id="minute" style="width:60px;" />
            <label style="width:1em;" for="minute">分</label>
            <input type="text" id="second" style="width:60px;" />
            <label style="width:1em;" for="second">秒</label>
            <input type="button" value="转换" class="btn btn-navbar" onclick="getUnixTime()"/>
            转化后结果<span id="DateToUnixTime" style="width:200px;display: inline-block;background: #ABCDEF;"> </span>
        <p> 
      </div>
    </div>
  </div>
</body>
</html>