JS时间戳转换时间,时间转时间戳源码

180it 2021-09-03 PM 994℃ 0条

JS时间戳转换时间,时间转时间戳源码

时间转时间戳源码:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>时间戳格式化</title><style>body{ margin:0; text-align:center}textarea{ margin:0; width:60%; height:200px;}
</style></head><body>


<p id="sjc" style='color: #C00;margin-top:30px;'></p><p id="sj" style='color: #C00;margin-top:30px;'></p>

<input id="stime" type='text' style=' width:500px; height:30px; color: #333; background-color:#fff; font-size:12px' value='' placeholder="输入时间戳" /><br /><br />

<input id="zh" type='button' style=' width:200px; height:60px; color: #FFF; background-color:#C30; font-size:20px' value='转换时间' onclick="" /><br /><br />

<input id="ztime" type='text' style=' width:500px; height:30px; color: #333; background-color:#fff; font-size:12px' value='' placeholder="转换结果" />



<script type="text/javascript">

var mydate = new Date();

var timestamp = parseInt(mydate.getTime());

window.onload =document.getElementById('stime').value = timestamp;

Date.prototype.Format = function (fmt) {

    var o = {

            "M+": this.getMonth() + 1, // 月份

            "d+": this.getDate(), // 日

            "h+": this.getHours(), // 小时

            "m+": this.getMinutes(), // 分

            "s+": this.getSeconds(), // 秒

            "q+": Math.floor((this.getMonth() + 3) / 3), // 季度

            "S": this.getMilliseconds() // 毫秒

    };

    if (/(y+)/.test(fmt))

        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + ""));

    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;

}

          
window.onload = window.setInterval(function (){

var mydate = new Date();

var timestamp = parseInt(mydate.getTime()/1000);

document.getElementById('sjc').innerHTML = timestamp;

document.getElementById('sj').innerHTML=new Date(mydate.getTime()).Format('yy-MM-dd hh:mm:ss'); //"2018-11-15 17:40:00"

}

,1000);

window.onload =document.getElementById('stime').innerHTML = document.getElementById('sjc').innerHTML;

  document.getElementById("zh").addEventListener("click", function(){

    var stime=document.getElementById('stime').value;

    if(stime.length==10){

    document.getElementById('ztime').value=new Date(parseInt(stime*1000)).Format('yy-MM-dd hh:mm:ss'); //"2018-11-15 17:40:00"

    }else if(stime.length==13){

    document.getElementById('ztime').value=new Date(parseInt(stime)).Format('yy-MM-dd hh:mm:ss'); //"2018-11-15 17:40:00"

    }else{

    alert("时间戳长度过长或过短!");

    }

    

  });

</script>

</body>

</html>

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

JS时间戳转换时间,时间转时间戳源码