Using MERN-stack build a app (MongoDB, ExpressJS, ReactJS, NodeJS(
我知道有很多关于堆栈溢出的文档/其他解决方案来解决类似的问题。
但是,让我对我的方案感到困惑的是,我没有创建一个new Date()
对象,然后渲染它。
我设置了一个后端模型,其中包含使用 Mongoose 的 Date 属性:
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)
现在我只是在组件/页面上呈现用户的数据,但它显示为
2020-05-10T17:57:14.987Z
您可以像这样使用moment
库:
文档在这里
const myTime = moment(dateFromDB).format('hh:mm:ss') // or any other format