<input type="text" id="orgvalue"/>
<input type="text" id="destvalue"/> <input type="button" onclick="toTime()" value="转时间"/> <input type="button" onclick="toTimeStamp()" value="转时间戳"/>
//转时间 function toTime() { var v= document.getElementById('orgvalue').value; document.getElementById('destvalue').value=timestampToTime(v) } //转时间戳 function toTimeStamp() { var v = document.getElementById('orgvalue').value; var date = new Date(v); document.getElementById('destvalue').value = date.getTime()/1000; } function timestampToTime(timestamp) { var date = new Date(timestamp * 1000);//时间戳为10位需*1000, var Y = date.getFullYear() + '-'; var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + var D = date.getDate() + ' '; var h = date.getHours() + ':'; var m = date.getMinutes() + ':'; var s = date.getSeconds(); return Y + M + D + h + m + s; }
不带毫秒数