1

As of now I can get the json format of my data using this code

    var bookingReserve = ['{!! $booking !!}'];
    var json = JSON.parse(bookingReserve);
    console.log(json);

and output like this

enter image description here

What I need to do is to generate json format based on the data thrown by bookingReserve

and

set date format Y-m-d to starts_at and ends_at

Unfortunately I can't filter the date format of the fields using laravel as I ask here

Convert DATE_FORMAT to Y-m-d? Using Larave Eloquent Raw Select

So what I'm trying to do is to generate json format and set Y-m-d date format for both fields using javascript

1
  • The ideal solution is to have your backend use a date format that JavaScript can understand, eg ISO 8601 Commented May 14, 2020 at 2:36

1 Answer 1

2
const formatDate = dateString => {
    const d = new Date(dateString)
    return `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}`
}

formatted = json.map(obj =>  {
    return {
        starts_at: formatDate(obj.starts_at),
        ends_at: formatDate(obj.ends_at)
    }

})

Or with your case, just need to trim after space.

formatted = json.map(obj =>  {
    return {
        starts_at: obj.starts_at.split(" ")[0],
        ends_at: obj.ends_at.split(" ")[0]
    }

})
Sign up to request clarification or add additional context in comments.

1 Comment

Been working on this since yesterday spent multiple hours. You saved me today! Thankyou

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.