From timestamp (int64) to date (string)

Hey folks :slight_smile:

Context

I am coming today for something which I think might be a small bug, but I let you confirm so :slight_smile:

I have a collection of models (mongodb) that are items with X, Y, Z fields, etc (doesn’t matter). Two of them tho are relatively created_at and updated_at.These two fields are int64 but for display reason, I want to converter them, however, it doesn’t work as expected:

Situation

I have both of my fields created_at and updated_at set with the value of 1611245643. Once converted, this value should return 21 Jan 2021 17:14:3 (and based on “logs”, this method does work and return the expected result).

However, the following is appearing:

tmp

Code

In terms of code, I have the following:

utils/converters.js

function fromTimestampToDate_RFC3339(UNIX_timestamp) {
    let a = new Date(UNIX_timestamp * 1000);
    let months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    let year = a.getFullYear();
    let month = months[a.getMonth()];
    let date = a.getDate();
    let hour = a.getHours();
    let min = a.getMinutes();
    let sec = a.getSeconds();
    let time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec;
    console.log("<"+UNIX_timestamp+"><"+time+">")
    return time
}

module.exports = {
    fromTimestampToDate_RFC3339: fromTimestampToDate_RFC3339,
}

models/items.js

// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
const mongoose = require('mongoose');

// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = mongoose.Schema({
   // ...
    'created_at': Number,
    // ...
    'updated_at': Number,
   // ...
}, {
    timestamps: false,
});

module.exports = mongoose.model('items', schema, 'Items');

forest/items.js

const {collection} = require('forest-express-mongoose');
const {fromTimestampToDate_RFC3339} = require('../utils/converters');

// This file allows you to add to your Forest UI:
// - Smart actions: https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions
// - Smart fields: https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields
// - Smart relationships: https://docs.forestadmin.com/documentation/reference-guide/relationships/create-a-smart-relationship
// - Smart segments: https://docs.forestadmin.com/documentation/reference-guide/segments/smart-segments
collection('items', {
    actions: [{
        // ...
    }, {
        // ...
    }],
    fields: [{
       // ...
    }  {
       // ...
    }, {
        field: 'created_at',
        type: 'String',
        get: (item) => {
            return fromTimestampToDate_RFC3339(item.created_at);
        }
    }, {
        field: 'updated_at',
        type: 'String',
        get: (item) => {
            return fromTimestampToDate_RFC3339(item.updated_at);
        }
    }],
    segments: [],
});

Any idea? :slight_smile: Thanks!

Max

1 Like

Hi @Emixam23 :wave:t3:

Sounds like your Smart Fields are hidden - could you try switching the Layout Editor on and checking?

What you’ve done here is create a virtual field in Forest Admin. Another way to achieve what you want is to use a hook directly on your schema to transform existing created_at and updated_at fields or override the default routes.

Let me know if this helps :pray:t3:

Hey :slight_smile:

No, they are not hidden, it’s just that, for some reason i don’t get the full date displayed but only a part of it… You talked about overriding the route, but could you provide me an example? if this solution is more suitable and do work, then I will go that way :slight_smile:

Thanks,

Max

Hey @Emixam23 it’s simply because your field must be declared as Number while you are actually displaying a date :sweat_smile:. So 21 Jan to Number gives 21 :wink:

What you should do is create your smart field with another name:

const {collection} = require('forest-express-mongoose');
const {fromTimestampToDate_RFC3339} = require('../utils/converters');

// This file allows you to add to your Forest UI:
// - Smart actions: https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions
// - Smart fields: https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields
// - Smart relationships: https://docs.forestadmin.com/documentation/reference-guide/relationships/create-a-smart-relationship
// - Smart segments: https://docs.forestadmin.com/documentation/reference-guide/segments/smart-segments
collection('items', {
    fields: [{
        field: 'smart_created_at', // See here I just renamed it to avoid conflict
        type: 'String', // You could use date here to have a more flexible display
        get: (item) => {
            return fromTimestampToDate_RFC3339(item.created_at);
        }
    }, {
        field: 'smart_updated_at',
        type: 'String',
        get: (item) => {
            return fromTimestampToDate_RFC3339(item.updated_at);
        }
    }],
});

I hope this fix your issue :wink:

1 Like

Thanks :slight_smile:

We can’t mark multiple message as “the solution” but both of them (combined) are the answer :slight_smile:

Appreciate!

Max

1 Like

Glad it’s working, no worries @Emixam23 :grinning_face_with_smiling_eyes:

I’d also take a look at mongoose getters/setters if I were you - Mongoose v5.11.15: Mongoose Tutorials: Getters/Setters in Mongoose

This will save you a few Smart Fields and likely improve performance :slight_smile:

Cheers,

1 Like