JS 时间格式转换

方便自己使用
export function dateToStr(datetime){ 
     datetime.setDate(datetime.getDate());
     var year = datetime.getFullYear();
     var month = datetime.getMonth()+1;//js从0开始取 
     var date = datetime.getDate(); 
     var hour = datetime.getHours(); 
     var m = datetime.getMinutes();
     month = month < 10 ?  "0" + month : month ;
     date  = date<10 ?  "0" + date : date;
     hour  = hour <10 ? "0" + hour : hour;
     return year + "-" + month + "-" + date + ' ' + hour + ':' + m ;
}
/*     时分秒      */
export function getDuration(my_time) {  
  var days    = my_time / 1000 / 60 / 60 / 24;
  var daysRound = Math.floor(days);
  var hours = my_time / 1000 / 60 / 60 - (24 * daysRound);
  var hoursRound = Math.floor(hours);
  var minutes = my_time / 1000 / 60 - (24 * 60 * daysRound) - (60 * hoursRound);
  var minutesRound = Math.floor(minutes);
  var seconds = my_time / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
  // console.log('转换时间:', daysRound + '天', hoursRound + '时', minutesRound + '分', seconds + '秒');
  var time =  ( hoursRound >= 10 ? hoursRound : '0' + parseInt(hoursRound)) + ':' + ( minutesRound >= 10 ? minutesRound : '0' + parseInt(minutesRound))  + ':' + (seconds >= 10 ? parseInt(seconds) : '0' + parseInt(seconds))
  if(minutesRound == 0 && seconds == 0){
    return false
  }
  return time;
 }

你可能感兴趣的:(JS 时间格式转换)