How to parse all dates in response in Integromat app?

373 Views Asked by At

In response, I get a big object with many data fields. I need a function that will be going through all properties of an object, and if it is a date, apply iml.parseDate(value) for this field. This function must be universal for other modules. I've noticed that typeof(date) returns " string" so I don't know how to do this.

[
    {
        "id": 13965629,
        "updated_at": "2020-07-10 02:01 PM +0300",
        "created_at": "2020-07-10 02:01 PM +0300",
        "creator": {
            "name":"Jon Doe",
            "registered": "2020-06-22 12:31 PM +0100",
        }
    }
]
1

There are 1 best solutions below

0
On

You can interate through your objects or arrays and match the date using regexp.

E.g.

function convertDates(obj) {
    Object.keys(obj).forEach(key => {
        let val = obj[key];

        if (Array.isArray(val)) {
            val.forEach(convertDates);
        }
        
        else if (typeof val === 'object') {
            return convertDates(val);
        }
        
        else if (/\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}\s+(?:PM|AM)\s+\+\d{4}/.test(val)) {
            obj[key] = parseDate(val);
        }
    });
    
    return obj;
}