js时间戳转化为日期格式,日期格式转化为时间戳

    <script>
        //将时间戳转化为日期格式
        var timestamp = 1076378315;
        function formatDate (now) {
          var year = now.getFullYear();
          var month = now.getMonth()+1;
          var date = now.getDate();
          var hour = now.getHours();
          var minute = now.getMinutes();
          return year + "/" + month + "/" + date + " " + hour + ":" + minute;
        }
        var d = new Date(timestamp);
        console.log(formatDate(d));//1970/1/13 18:59
        //将日期格式转化为时间戳
        //1.将现在的时间转化为时间戳
        var timestamp1 = (new Date()).valueOf();//通过valueOf()函数返回指定对象的原始值获得准确的时间戳值
        console.log(timestamp1);//1558431301009
        var timestamp2 = new Date().getTime(); //通过原型方法直接获得当前时间的毫秒值,准确
        console.log(timestamp2);//1558431301009
        var timestamp3 = Number(new Date());//将时间转化为一个number类型的数值,即时间戳
        console.log(timestamp3);//1558431301010
      </script>

 

posted @ 2019-05-21 17:42  巴拉巴拉·芭芭拉  阅读(532)  评论(0编辑  收藏  举报