castDates and formatDates are not working

Im trying to format my created at to DD/MM/YYYY but is not working.
did I forget something?

My Model

'use strict'
const moment = use('moment')

/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')

class Cupom extends Model {

    static get dates() {
return super.dates.concat(["created_at"]);

static castDates(field, value) {
if (field == "created_at") return value ? value.format("DD/MM/YYYY") : value;
else return value ? value.format("DD/MM/YYYY HH:mm:ss") : value;
// else used for created_at / updated_at

static formatDates(field, value) {
if (field == "created_at")
  // format only certain fields
  return moment(value, "DD/MM/YYYY").format("YYYY-MM-DD");

return super.formatDates(field, value);

    usuario () {
        return this.belongsTo('App/Models/User')

module.exports = Cupom

My Controller

async index ({ request, response, view }) {

    const cupons = await'*').from('cupoms')

      return view.render('cupons', {
      cupons: cupons


My View

@each(cupom in cupons)

1 Like
  1. The created_at field created via table.increment() is a date field by default. So you should not return it again to the dates array. See

  2. You did not return super.formatDates(field, value) in the castDates method.

Aside from that I do not see any other issue.

1 Like

Recently, I have been having this same issue with castDates(). After going through the Base.js file, I decided to use getters and it solved my problem.

To return the updated_at and created_at fields to “YYYY-MM-DD” format, I used:

const moment = use('moment')

getUpdatedAt(date) {

    return moment(date).format("YYYY-MM-DD");


  getCreatedAt(date) {

    return moment(date).format("YYYY-MM-DD");


My issue is solved. I will totally abandon formatDates and castDates going forward until the maintainers point out why it is so difficult to use them.

Meanwhile, this is the implementation of formatDates in Base.js:

static formatDates (key, value) {

    return moment(value).format(DATE_FORMAT)


const DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss'

I think formatDates should provide a parameter to override the default DATE_FORMAT.

Something like:

static formatDates (key, value, format) {

    const _format = format ? format : DATE_FORMAT
    return moment(value).format(_format)