如果我们在操作小程序云函数数据的时候有插入时间,那么得到的时间格式是这样的
如果我们要把这个时间进行展示的话,需要对这个时间进行格式化处理, 为了方便大家复用,我把它拿出来单独创建一个js文件进行引用它,处理的方法如下:
在小程序项目中创建一个js文件
方法如下:
module.exports = (date) => {
let fmt = 'yyyy-MM-dd hh:mm:ss'
const o = {
'M+': date.getMonth() + 1, // 月份
'd+': date.getDate(), //日
'h+': date.getHours(), //小时
'm+': date.getMinutes(), //分钟
's+': date.getSeconds() //秒
}
if(/(y+)/.test(fmt)){
fmt = fmt.replace(RegExp.$1,date.getFullYear())
}
for(let k in o){
if(new RegExp('('+k+')').test(fmt)){
fmt = fmt.replace(RegExp.$1, o[k].toString().length == 1 ? '0' + o[k] : o[k])
}
}
// console.log(fmt)
return fmt
}
然后在你需要的地方进行引用
// components/blog-card/blog-card.js
import formatTime from '../../utils/formatTime.js'
Component({
/**
* 组件的属性列表
*/
properties: {
blog: Object
},
//数据监听器
observers: {
//格式化时间
['blog.createTime'](val){
if(val){
this.setData({
_createTime: formatTime(new Date(val))
})
}
}
},
/**
* 组件的初始数据
*/
data: {
_createTime:''
},
处理后的结果如下:
收工