gpt4 book ai didi

javascript - 使用 'moment.js' 将自定义日期戳添加到 node.js

转载 作者:行者123 更新时间:2023-12-03 02:45:46 26 4
gpt4 key购买 nike

如何正确使用moment.js?我想使用表单将创建日期保存到mongoDB?我目前正在使用:

var blogSchema = new mongoose.Schema({
title: String,
image: String,
body: String,
created: {type: Date, default: Date.now}
});

当在博客文章上显示日期时,我使用以下命令将其转换为更易读的格式:

<span class="inline-block"><%= blog.created.toDateString() %></span>

提交帖子时如何将当前日期显示为“DD-MM-YYYY @ mm:hh”

最佳答案

假设从mongodb获取的日期值是时间戳值:

然后你可以像这样使用 moment.js :

var timeValueFromMongoDB = 1515089852632;
var result = moment(timeValueFromMongoDB).format('DD-MM-YYYY @ mm:hh');

// Use this output to display wherever you want
04-01-2018 @ 17:01

Jsfiddle 示例 http://jsfiddle.net/rLjQx/5208/

关于javascript - 使用 'moment.js' 将自定义日期戳添加到 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48101182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com