util.js 1.14 KB
// const formatTime = timeStr => {
 
function formatTime(timeStr,format){
  // var format = "date";
    // console.log(format)
  var twoNum = function twoNum(num) {
    return ('0' + num).slice(-2);
  };
  var tArr = timeStr.split(/\D+/).map(Number);
  var tArr = timeStr.split(/\D+/).map(Number);
  switch (tArr.length) {
    case 5:
      tArr.push(0);break;
    case 4:
      tArr.push(0, 0);break;
    case 3:
      tArr.push(-8, 0, 0);break;
    case 2:
      tArr.push(1, -8, 0, 0);break;
    case 1:
      tArr.push(1, 1, -8, 0, 0);break;
    default:
      break;
  }
  var myDate = new Date(tArr[0], tArr[1] - 1, tArr[2], tArr[3] + 8, tArr[4], tArr[5]);
  switch (format) {
    case 'date':
      return myDate.getFullYear() + '-' + twoNum(myDate.getMonth() + 1) + '-' + twoNum(myDate.getDate());
    case 'full':
      return myDate.getFullYear() + '-' + twoNum(myDate.getMonth() + 1) + '-' + twoNum(myDate.getDate()) + ' ' + twoNum(myDate.getHours()) + ':' + twoNum(myDate.getMinutes());
    case 'DateObj':
      return myDate;
  }
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime
}