钉钉小程序格式化日期

创建sjs文件

// pages/index/index.sjs
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const formatTime = (timestamp, format) => {
  var date = getDate(timestamp);
  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));
  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));
  for (var i = 0; i < returnArr.length; i++) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
};
export default {
  formatTime,
};

使用方法

<import-sjs name="m1" from="../../../util/data.sjs"/>

日期:{{ m1.formatTime(date,'Y-M-D') }}

posted @ 2022-11-14 18:38  Sqsdhc  阅读(176)  评论(0)    收藏  举报