【问题标题】:React, MongoDB, Mongoose: How to format my date to be readable?React、MongoDB、Mongoose:如何格式化我的日期以使其可读?
【发布时间】:2020-10-04 07:56:05
【问题描述】:

使用 MERN-stack 构建应用程序(MongoDB、ExpressJS、ReactJS、NodeJS)

我知道 stackoverflow 上有很多针对类似问题的文档/其他解决方案。

但是,我对我的场景感到困惑的是,我没有创建 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

【问题讨论】:

    标签: javascript reactjs mongodb mongoose mern


    【解决方案1】:

    您可以像这样使用moment 库:

    Documentation here

    const myTime = moment(dateFromDB).format('hh:mm:ss') // or any other format
    

    【讨论】:

      猜你喜欢
      • 2011-12-27
      • 1970-01-01
      • 2011-11-18
      • 2012-01-30
      • 2012-08-03
      • 2020-11-13
      • 2014-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多