猫鼬日期模式 [英] Mongoose Date Schema

查看:82
本文介绍了猫鼬日期模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MongoDB,Mongoose,Express和Express Handlebars.

I am using MongoDB, Mongoose, Express and Express Handlebars.

我的表单中有一个<input type="date">,它发布到数据库中,并且我的猫鼬模式是这样定义的:

I have an <input type="date"> in my form, that posts to database, and my mongoose schema is defined like this:

var recordsSchema = new mongoose.Schema ({
   scheduled: Date,
});

该表格会插入时间,但是输出看起来像这样:

The form inserts the time, but the output looks something like this:

2018年10月26日星期五00:00:00 GMT + 0000(UTC)

Fri Oct 26 2018 00:00:00 GMT+0000 (UTC)

我想对其进行格式化,使其显示为:

I want to format it so it appears as:

2018年10月26日星期五

Fri Oct 26 2018

别无其他.

我尝试在Handlebars模板中使用toString()方法,但是它不起作用,服务器抛出错误.

I tried using toString() method in the Handlebars template, but it doesn't work and server throws an error.

这是Express-Handlebars上的样子:

This is how it looks on the Express-Handlebars side:

{{ this.scheduled }} {{ this.scheduled.toString() }}

有人可以让我知道如何解决这个问题吗?

Can someone please let me know how to solve this?

谢谢.

推荐答案

尝试在架构中添加自定义getter以获取预期的格式化日期字符串.

Try to add a custom getter in the schema to get the formatted date string as expected.

var recordsSchema = new Schema ({
  scheduled: Date,
  get: value => value.toDateString()
});

这篇关于猫鼬日期模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆