简体   繁体   中英

Format Date() for express-handlebars

I want to display the date in the format "YYYY MM DD".

I have tried with expresss-handlebars code, {{this.createdAt.toDateString()}}

DB/Post.js

username: String,
    createdAt: {
        type: Date,
        default: new Date()
    }

index.js

app.get('/', async (req, res)=>{
    const posts = await Post.find().sort({"createdAt": -1})
    res.render('index', {
        posts
    });
})

post.handlebars

{{this.createdAt.toDateString()}} 

Use:

{{this.createdAt.toLocaleDateString('EU'))}};

this will return YYYY-MM-DD, if you want to drop the - add split and join:

{{this.createdAt.toLocaleDateString('EU')).split('-').join(' ')}};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM