小程序
小游戏
企业微信
微信支付
扫描小程序码分享
图1是我想在两个view中分别显示name和date:
图2是运行界面,发现date类型的数据显示不出来貌似是我写法不对?请教各位该咋写
2 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
// wxml <wxs module="filter"> function parseDate(dateStr) { if (!dateStr) { return dateStr } var date = getDate(dateStr) var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() var hour = date.getHours() var minute = date.getMinutes() var second = date.getSeconds() var formatNumber = function(n) { n = n.toString() if (n[1]) { return n } else { return "0" + n[0] } } return year + "-" + formatNumber(month) + "-" + formatNumber(day) + " " + formatNumber(hour) + ":" + formatNumber(minute) + ":" + formatNumber(second) } module.exports = { parseDate: parseDate } </wxs> <view wx:for="{{dataObj}}" wx:key="_id"> <text>name: {{item.name}} ---- </text> <text>date: {{filter.parseDate(item.date)}}</text> </view> // js Page({ data: { dataObj: [{ date: "2022-04-17T09:51:53.815Z", name: "abc", _id: "1" }, { date: "2022-02-17T09:51:53.815Z", name: "def", _id: "2" }], } })
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
第二种
// js
formatDate(date) {
if (!date) {
return date
}
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
var formatNumber = function(n) {
n = n.toString()
if (n[1]) {
return n
} else {
return "0" + n[0]
return year + "-" + formatNumber(month) + "-" + formatNumber(day) + " " + formatNumber(hour) + ":" + formatNumber(minute) + ":" + formatNumber(second)
},
onLoad() {
let dataObj = this.data.dataObj
dataObj.forEach(item => {
item.date = new Date(item.date)
})
this.setData({
dataObj
用第一种全是 nan !
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
// wxml <wxs module="filter"> function parseDate(dateStr) { if (!dateStr) { return dateStr } var date = getDate(dateStr) var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() var hour = date.getHours() var minute = date.getMinutes() var second = date.getSeconds() var formatNumber = function(n) { n = n.toString() if (n[1]) { return n } else { return "0" + n[0] } } return year + "-" + formatNumber(month) + "-" + formatNumber(day) + " " + formatNumber(hour) + ":" + formatNumber(minute) + ":" + formatNumber(second) } module.exports = { parseDate: parseDate } </wxs> <view wx:for="{{dataObj}}" wx:key="_id"> <text>name: {{item.name}} ---- </text> <text>date: {{filter.parseDate(item.date)}}</text> </view> // js Page({ data: { dataObj: [{ date: "2022-04-17T09:51:53.815Z", name: "abc", _id: "1" }, { date: "2022-02-17T09:51:53.815Z", name: "def", _id: "2" }], } })
第二种
// js
formatDate(date) {
if (!date) {
return date
}
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
var formatNumber = function(n) {
n = n.toString()
if (n[1]) {
return n
} else {
return "0" + n[0]
}
}
return year + "-" + formatNumber(month) + "-" + formatNumber(day) + " " + formatNumber(hour) + ":" + formatNumber(minute) + ":" + formatNumber(second)
},
onLoad() {
let dataObj = this.data.dataObj
dataObj.forEach(item => {
item.date = new Date(item.date)
})
this.setData({
dataObj
})
}
用第一种全是 nan !