将时间戳转化为具体时分秒

/*******************util.js*********************/ 

//将时间戳转化成时分秒的形式

function getZero(num){ 

  // 单数前面加0

  if(parseInt(num) < 10){ 

    num = '0'+num; 

  } 

  return num; 

}

function formatDate(now) {

  var year=now.getFullYear(); 

  var month=now.getMonth()+1;

  var date=now.getDate();   

  var hour=now.getHours();   

  var minute=now.getMinutes();

  // var second=now.getSeconds();

  return year+"."+getZero(month)+"."+getZero(date)+" "+getZero(hour)+":"+getZero(minute);

  }

  function formatDates(now) {

    var year=now.getFullYear(); 

    var month=now.getMonth()+1;

    var date=now.getDate();   

    var hour=now.getHours();   

    var minute=now.getMinutes();

    // var second=now.getSeconds();

    return year.toString()+getZero(month).toString()+getZero(date).toString()+getZero(hour).toString()+getZero(minute).toString();

    }

  module.exports = {

    formatDate: formatDate,

    formatDates: formatDates,

  }

/***********************使用方法*********************/

const formatDate = require('../../utils/util.js').formatDate;

//获取当前时间戳 const timestamp = Date.parse(new Date());

//创建一个指定的日期对象 var d=new Date(timestamp);

//输出具体时间 console.log( formatDate(d))


你可能感兴趣的:(将时间戳转化为具体时分秒)