React,MongoDB,Mongoose:如何格式化我的日期以便阅读?
React, MongoDB, Mongoose: How to format my date to be readable?
使用 MERN-stack 构建应用程序(MongoDB、ExpressJS、ReactJS、NodeJS)
我知道在 Whosebug 上有很多针对类似问题的 docs/other 解决方案。
但是,让我对我的场景感到困惑的是我没有创建 new Date()
对象然后渲染它。
我使用 Mongoose 设置了一个具有日期属性的后端模型:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
username: {
type: String,
required: true,
unique: true
},
date: {
type: Date,
default: Date.now
}
})
module.exports = User = mongoose.model('user', UserSchema)
现在我只是在 component/page 上呈现用户数据,但结果显示为
2020-05-10T17:57:14.987Z
您可以像这样使用 moment
库:
const myTime = moment(dateFromDB).format('hh:mm:ss') // or any other format
使用 MERN-stack 构建应用程序(MongoDB、ExpressJS、ReactJS、NodeJS)
我知道在 Whosebug 上有很多针对类似问题的 docs/other 解决方案。
但是,让我对我的场景感到困惑的是我没有创建 new Date()
对象然后渲染它。
我使用 Mongoose 设置了一个具有日期属性的后端模型:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
username: {
type: String,
required: true,
unique: true
},
date: {
type: Date,
default: Date.now
}
})
module.exports = User = mongoose.model('user', UserSchema)
现在我只是在 component/page 上呈现用户数据,但结果显示为
2020-05-10T17:57:14.987Z
您可以像这样使用 moment
库:
const myTime = moment(dateFromDB).format('hh:mm:ss') // or any other format