js格式化時間戳

項目中,前端接收到後臺返回的時間不是正常的格式,而是相似於這種:1560312661894前端

第一步,先轉換成日期格式,以下.net

new Date(1560312661894);orm

第二步,帶格式化成想要的格式,以下:get

/**
         * 格式化時間戳
         * @param {Object} now
         */
        function formatDate(now) {
          var year = now.getFullYear(),
          month = now.getMonth() + 1,
          date = now.getDate(),
          hour = now.getHours(),
          minute = now.getMinutes(),
          second = now.getSeconds();
         
          return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
        }io

相關文章
相關標籤/搜索