Formatting a date from a jsonobject placed in a single cell.

Formatting a date from a jsonobject placed in a single cell.

ReritanReritan Posts: 2Questions: 1Answers: 0

Hello. I use Laravel. I have a table with fields: id, date, admin, data. The data field is a json comment object stored in a cell of my database.
What does the object stored in the database look like:

{"comment":{"user_id":"1","text":"888888888888","admin_id":1,"updated_at":"2022-07-07T08:05:35.000000Z","created_at":"2022-07-07T08:05:35.000000Z","id":3}}

I ran into the problem of formatting the date in this object. I need to convert 2022-07-07T08:05:35.000000Z to 2022-07-07, 08:05:35. What are the possibilities for this.
The difficulty is that in my table I get this object as a formatted string wrapped in a

<

pre> tag. And to do the formatting, you need to parse this string. Is there an easy way to do this formatting?
The data that I get in my table:

{ id: 2, created_at: "2022-07-05 08:05:35", action: "add_comment", data: "<pre>comment\n\tuser_id:\t1\n\ttext:\t888888888888\n\tadmin_id:\t1\n\tupdated_at:\t2022-07-07T08:05:35.000000Z\n\tcreated_at:\t2022-07-07T08:05:35.000000Z\n\tid:\t3\n</pre>", admin_id: 1, name: "Admin" }

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,345Questions: 26Answers: 4,954
    Answer ✓

    I would look at using regex to get the string.
    Looks like this SO thread is asking something similar. You can use this in columns.render to extract and return the data for the column.

    Kevin

  • ReritanReritan Posts: 2Questions: 1Answers: 0

    kthorngren Thanks a lot!
    My code:

    {
                        data: 'data',
                        "render": function (data) {
                            let withFormatDate = data.replace(/(\d+?-\d+?-\d+?)T(\d+?)/g, data => data.replace('T', ', '));
                            return withFormatDate.replace(/(\d+?:\d+?:\d+?).(\d+?Z)/g,
                                    withFormatDate => withFormatDate.substr(0,withFormatDate.indexOf(".")));
                        },
                    }
    
Sign In or Register to comment.