欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js时间戳日期转换

程序员文章站 2022-03-29 18:33:21
...

时间戳转换js工具类

// 时间戳转换日期
function add0(m){
return m<10?'0'+m:m 
}

/**
 *  时间戳转 Date 2020年6月30日20点53分
 * @param timeStamp
 * @returns {string}
 */
function format(timeStamp){
   var result = "";
   if (timeStamp != undefined) {
      //timeStamp是整数,否则要parseInt转换
      var time = new Date(timeStamp);
      var y = time.getFullYear();
      var m = time.getMonth()+1;
      var d = time.getDate();
      var h = time.getHours();
      var mm = time.getMinutes();
      var s = time.getSeconds();

      result = y+'年'+add0(m)+'月'+add0(d)+'日'+add0(h)+'时'+add0(mm)+'分';
   }
   return result;
}


/**
 *  时间戳转 Date 2020-06-30 20:53
 * @param timeStamp
 * @returns {string}
 */
function formatToDate(timeStamp){// eg: 2020-06-30 20:53
   var result = "";
   if (timeStamp != undefined) {
      //timeStamp是整数,否则要parseInt转换
      var time = new Date(timeStamp);
      var y = time.getFullYear();
      var m = time.getMonth()+1;
      var d = time.getDate();
      var h = time.getHours();
      var mm = time.getMinutes();
      var s = time.getSeconds();

      result = y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm);
   }
   return result;
}
相关标签: 工具类(代码)