Convert created_at value to another timezone before saving

I have a query to how to convert the created_at date value to any specific timezone (ex. UTC) while saving the new record.

  • I am using the increments/timestamp to create the created_at field in the database. The values of created_at and updated_at fields will be generated according to the server timezone by the adonis. But i need to change the timezone of the created_at value before saving the record.

  • I have already tried a solution found in knex.js but it is also not working.

connection: {

timezone: ‘UTC’,
typeCast: function (field, next) {
if (field.type == ‘DATETIME’) {
return moment(field.string()).format(‘YYYY-MM-DD HH:mm:ss’);
}
return next();
}
}

Thanks in advance!

1 Like

Up! I’ve tried with adding next pool value to the connection object in config/database.js and it’s not working:

 pool: {
        afterCreate: function(connection, callback) {
          connection.query('SET time_zone = "UTC+4";', function(err) {
            callback(err, connection);
          });
        }
      }

i’ts also funny because adding " timezone: ‘-4:00’ " right before pool at the same connection objetct… register started to change created at, and did some time traveling hahaha Captura%20de%20pantalla%20de%202019-08-14%2003-04-07

Please help! Is there any configuration, maybe at Lucid models or somewhere else, where we can set the timestamps to desired timezone?

Why not use dateformats https://adonisjs.com/docs/4.1/lucid#_formatting_date_fields

1 Like