使用ejs从mongo进行编辑时显示日期 [英] display a date on an edit from from mongo using ejs

查看:84
本文介绍了使用ejs从mongo进行编辑时显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个编辑页面,用户可以在其中显然地编辑事件的各个方面。除日期显示为空外,其他所有内容均显示正常。

There is an edit page where a user can, obviously, edit aspects of an event. Everything displays fine except the Date shows empty.

我在html中使用了 type = date ,这可能是一个原因,我该如何获取围绕此操作,以便我可以显示日期,因为编辑事件后,保存时它保存为null

I am using the type="date" in my html which may be a cause, how can I get around this so that I can show the date, because when it saves it saves as null after editing the event

视图:

 <div class="form-group">
    <input class="form-control" type="date" name="editEvent[startDate]" placeholder="Date" value="<%= event.startDate %>">
 </div>

路线:

router.get("/event/:id/edit", isLoggedIn, function(req,res){
Event.findById(req.params.id, function (err, foundEvent) {
    if(err){
        console.log(err);
        console.log("Cannot find the event...maybe the database isnt running?")
    } else {
        res.render("eventEdit", {event: foundEvent});

一切正常可以,但是可以使用日期

Everything works fine but the date

推荐答案

事实证明,它是HTML格式的东西,如果要显示日期,则必须设置其格式如 YYYY-MM-DD 所示,则它可以正常显示,否则不能识别该格式。

It turns out its an HTML thing, if you want to display a date you have to have it formattted as YYYY-MM-DD then it shows fine, otherwise it doenst recognize the format.

如果您在输入中使用 type = date 属性

This comes into account if you use the type="date" attribute in your input

我必须编辑事件日期mongo使用momentjs,这是我使用的行。

I had to edit the event date coming from mongo using momentjs and here is the line I used.

var newDate = moment(foundEvent.startDate).utc()。format( Y YYY-MM-DD)
res.render( eventEdit,{event:foundEvent,newDate:newDate});

newDate 是被传递到模板的变量,然后我只有:

The newDate is the variable being passed to the template, then I just have:

 <input class="form-control" type="date" name="editEvent[startDate]" placeholder="Date" value="<%= newDate %>">

这有效。

这篇关于使用ejs从mongo进行编辑时显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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