需求做了,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>
