小程序时间戳转换为日期

  • 关于 timeStamp 语法,参考 https://developers.weixin.qq.com/miniprogram/dev/api/open-api/payment/wx.requestPayment.html

小程序时间戳转换为日期

1.导出

小程序时间戳转换为日期_第1张图片

const formatTime = date => {
  var date = new Date()//一定要记得写这个,不然会报date.getFullYear is not a function
  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,
}

2.导入

const time = require("../../utils/util.js");//根据自己项目的位置而定

3.使用

var currenttime= res.data.timeStamp;
console.log(time.formatTime(currenttime, 'Y/M/D h:m:s'));

小程序时间戳转换为日期_第2张图片

4.注意了喔 date.getFullYear is not a function 是因为util.js中没有写下面一行代码

在这里插入图片描述

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