node.js - 从mongodb获取数据时如何使用momentjs格式化时间
问题描述
我不知道如何使用从 MongoDB 获取数据的时刻来格式化时间
我试过了<%= m(blog.created).format('MMMM Do YYYY, h:mm:ss a')%>
var moment = require('moment');
var m = moment();
这是在我的 app.js 中设置架构
var blogSchema = new mongoose.Schema({
title: String,
author: String,
body: String,
image: String,
created: {type:Date, default:Date.now}
});
在我的 Show.ejs 页面上
<div class="ui text main container segment">
<div class="ui huge header"><%=blog.title%></div>
<div class="ui top attached">
<div class="item">
<img class="ui centered rounded image" src="<%=blog.image%>">
<div class="content">
<span><%=blog.created%></span>
</div>
<div class="description">
<p><%=blog.body%></p>
</div>
</div>
</div>
</div>
我希望日期格式是这样的
moment().format('MMMM 做 YYYY, h:mm:ss a'); // 2019 年 2 月 6 日,凌晨 2:31:46
解决方案
试试这个:
var moment = require('moment');
app.locals.moment = require('moment'); // this makes moment available as a variable in every EJS page
在 Show.ejs 上
<%= moment(blog.created).format('MMMM Do YYYY, h:mm:ss a')%>
推荐阅读
- php - Laravel 表单提交路由错误 - 此路由不支持 POST 方法
- azure - 我可以将我的语音集成到 Microsoft 团队中的文本翻译应用程序吗?
- c++ - 为什么这些代码仍然使用左值函数?
- database - 编译 gem 'sqlite3' ruby 项目时出错
- vba - VBA-以奇怪的方式定义的变量类型
- javascript - 如何删除 Jquery 在 textarea 中插入的逗号?
- c# - 如何从文本中删除任何给定的字符串对
- javascript - 文本区域中的实时计数行
- python - 如何解决这个问题?def _create_frequency_table(text_string)->dict: ^ SyntaxError: 无效语法
- matlab - 如何在 MATLAB 中显式地制作数组的深层副本?