微信小程序获取时间对象

146 阅读1分钟

.wxs文件

function supp(n) {
  return n < 10 ? ('0' + n) : n
}
function transTime() {
  //微信小程序用getDate()获取时间对象
  var oDate = getDate();
  
  console.log(oDate);
  var y = oDate.getFullYear();
  var m = oDate.getMonth() + 1;
  var r = oDate.getDate();
  var h = oDate.getHours();
  var f = oDate.getMinutes();
  var s = oDate.getSeconds();
  var str = y + '年' + supp(m) + '月' + supp(r) + '日' + supp(h) + '小时' + supp(f) + '分钟' + supp(s) + '秒';
  return str;
}

module.exports = {
  formTime: transTime
}

在.wxml中调用

<wxs src="../../utils/tools.wxs" module="aaa"></wxs>
<view>{{aaa.formTime()}}</view>