0

My CRM send the following JSON to my webhook:

"last_conversion":
            {
                "content":
                {
                    "identifier":"conv-list",
                    "created_at":"2020-05-09T22:06:29.049670Z",
                    "Name":"Test",
                    "BusinessName":"Test Business",
                    "Phone":"+201 (99) 9999-9999",
                    "gclid_field":"232938293"
                },
                "created_at":"2020-05-09T19:06:29.049-05:00"
}

Sending information to google ads requires a specific data format:

Google accepted formats

I would like to create a json with the following format:

{
"content_created_at":"2020-05-09T22:06:29-05:00",
"Name":"Test",
"created_at": "2020-05-09T19:06:29-05:00"
}  

To achieve that, i should change both created_at keys

1) 2020-05-09T22:06:29.049670Z to 2020-05-09T22:06:29-05:00 (Removing the .049670Z and adding -05:00)

2) 2020-05-09T19:06:29.049-05:00 to 2020-05-09T19:06:29-05:00 (Removing the .049)

What is the most elegant way to achieve that? I'm trying some kind of regex but is not working as intended

  • You can't use a regular expression to add the offset because a regular expression doesn't know what it is, or how to adjust the date and time values. You can convert the string to a Date and format it as required, there are many, many questions here already on formatting dates. – RobG May 10 '20 at 00:33

2 Answers2

2

You don't need to use regex here. Just convert the date string into a JS Date object:

const date = new Date("2020-05-09T22:06:29.049670Z")

Once you did this you can use the date methods to format your data.

For a more pleasant experience when working with dates in JS, you could also take a look at moment.js.

Patrick
  • 254
  • 2
  • 7
0

It is not fully clear what the full format of your data is so I have made an assumption that this is a part of a larger array such as:

data = [{
    "last_conversion": {
        "content": {
            "identifier": "conv-list",
            "created_at": "2020-05-09T22:06:29.049670Z",
            "Name": "Test",
            "BusinessName": "Test Business",
            "Phone": "+201 (99) 9999-9999",
            "gclid_field": "232938293"
        },
        "created_at": "2020-05-09T19:06:29.049-05:00"
    }
}];

Using an array such as above, you can format using something like this using toJSON() (or toISOString()) which will give you ISO formats of these dates:

updated = data.map(record => {
    return {
        Name: data[0].last_conversion.content.Name,
        created_at: new Date(data[0].last_conversion.created_at).toJSON(),
        content_created_at: new Date(data[0].last_conversion.content.created_at).toJSON()
    }
});

Which will result in data such as:

[{
    "Name": "Test",
    "created_at": "2020-05-10T00:06:29.049Z",
    "content_created_at": "2020-05-09T22:06:29.049Z"
}]

If that format is not accepted you can use the moment library or a similar date formatter library. Here is a possible data formatter:

// @credit to 
// https://stackoverflow.com/a/56709229/886437
// by https://stackoverflow.com/users/392145/nahuel-greco
function dateToLocalISO(date) {
    const off    = date.getTimezoneOffset()
    const absoff = Math.abs(off)
    return (new Date(date.getTime() - off*60*1000).toISOString().substr(0,23) +
            (off > 0 ? '-' : '+') + 
            (absoff / 60).toFixed(0).padStart(2,'0') + ':' + 
            (absoff % 60).toString().padStart(2,'0'))
}

updated = data.map(record => {
    return {
        Name: data[0].last_conversion.content.Name,
        created_at: dateToLocalISO(new Date(data[0].last_conversion.created_at)),
        content_created_at: new Date(data[0].last_conversion.content.created_at).toJSON()
    }
});
Johnny Rice
  • 1,737
  • 8
  • 14