小程序日期格式化

微信小程序——格式化时间


1. formatTime :Data型 2.formatTime1:Long型


util.js


----------


//两种方式 1.formatTime 传入参数 Date 返回:年/月/日
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

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

module.exports = {
  formatTime: formatTime,
  formatTime1: formatTime1
}



function   formatTime1   (n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 2.formatTime1传入参数 (时间戳,格式:如YYYY -MM-DD)
//返回:年-月-日(自定义)
function formatTime1(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber1(date.getMonth() + 1));
  returnArr.push(formatNumber1(date.getDate()));

  returnArr.push(formatNumber1(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber1(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

你可能感兴趣的:(微信小程序开发)