0

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()}} 
  • possible duplicate https://stackoverflow.com/questions/43591405/format-date-in-mongodb-query-output-on-shell – dege May 28 '19 at 13:01

1 Answers1

0

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(' ')}};

Tom Slabbaert
  • 10,710
  • 8
  • 21
  • 32