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

lmalmeida
  • 135
  • 2
  • 14
  • 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

1 Answers1

3

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
  • 340
  • 2
  • 8