helper.wxs 1.04 KB
function getDateList(date) {
  if (!date) {
    return []
  }

  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();

  return [year, month, day];
}

function formatN(format, num) {
  var n = num + '';
  var nList = n.split('')
  var l = nList.length;
  var fl = format.length;

  if (format.length < nList.length) {
    return nList;
  }

  var el = fl - l;

  for(var i = 0; i < el; i++) {
    nList.splice(0, 0, '0');
  }

  return nList.join('');
}

function formatTime(start, end) {
  var startTime = getDate(start * 1000);
  var endTime = getDate(end * 1000);

  var startDate = getDateList(startTime);
  var endDate = getDateList(endTime);

  var startDateFormat = startDate[0] + '.' + formatN('00', startDate[1]) + '.' + formatN('00', startDate[2])
  var endDateFormat =  '-' + formatN('00', endDate[1]) + '.' + formatN('00', endDate[2]);

  if (!end) {
    return "即将上线:" + startDateFormat;
  }

  return startDateFormat + endDateFormat;
}

module.exports = {
  formatTime: formatTime,
  formatN: formatN,
};