Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Needing help extracting a date from a string in Javascript

So I have an axios call that fetches an object that looks like this…

    {
        "meeting_id": "982a4867-2b3e-41a7-a5e2-22d86094c980",
        "meeting_topic": "GCA Help",
        "created_at": "2022-10-31T20:30:33.568Z",
        "updated_at": "2022-10-31T20:30:33.568Z",
        "meeting_start_time": "2022-07-09T02:01:19",
        "meeting_end_time": "2022-07-09T03:01:19",
        "mentor_id": "882eb36a-d154-480d-89d4-a1cad1aa7330",
        "mentee_id": "50ef4f37-b8bd-4c93-a9a3-625e38c2c5cb",
        "admin_meeting_notes": "Meeting notes here!",
        "mentor_meeting_notes": "Mentor meeting notes",
        "mentee_meeting_notes": "Mentee meeting notes",
        "meeting_missed_by_mentee": "Attended",
        "user_id": "63447ddfe85cb425b0fed28e",
        "profile_id": "50ef4f37-b8bd-4c93-a9a3-625e38c2c5cb",
        "role": "mentee"
    }

I need to take the "meeting_start_time" keys, value, and parse it into a string of "MM/DD/YYYY". This will be stored in state, and used to render events using the AntD Calendar component.

I have attempted a regex solution which was way to messy.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I don’t have access to the database schema to change the delivered format.

>Solution :

To do this you can:

  • parse property with Date constructor
  • use toLocaleString to convert to MM/DD/YYYY, …time format, then trim off time (by splitting the string by comma and getting first item)
const obj={meeting_id:"982a4867-2b3e-41a7-a5e2-22d86094c980",meeting_topic:"GCA Help",created_at:"2022-10-31T20:30:33.568Z",updated_at:"2022-10-31T20:30:33.568Z",meeting_start_time:"2022-07-09T02:01:19",meeting_end_time:"2022-07-09T03:01:19",mentor_id:"882eb36a-d154-480d-89d4-a1cad1aa7330",mentee_id:"50ef4f37-b8bd-4c93-a9a3-625e38c2c5cb",admin_meeting_notes:"Meeting notes here!",mentor_meeting_notes:"Mentor meeting notes",mentee_meeting_notes:"Mentee meeting notes",meeting_missed_by_mentee:"Attended",user_id:"63447ddfe85cb425b0fed28e",profile_id:"50ef4f37-b8bd-4c93-a9a3-625e38c2c5cb",role:"mentee"};

const date = new Date(obj.meeting_start_time).toLocaleString('en-US').split(',')[0]
console.log(date)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading