after a bit of guidance here as i think i’m getting myself confused 😉
I have an object, an example below:
{
"_id": "63dd35e801ad6ada9a5eeb08",
"name": "Joe Bloggs",
"technicalCertification": true,
"salesCertification": true,
"salesCertificationExpiry": "2023-12-27T12:02:31.043Z",
"techicalCertificationExpiry": "2024-09-29T12:02:48.254Z"
}
There may be more fields with the ISO date format, so to display this in a friendly manner i was trying to create a function such as:
const formatISODate = function (obj, field) {
let day = obj.data[field].split('T')[0].split('-')[2]
let month = obj.data[field].split('T')[0].split('-')[1]
let year = obj.data[field].split('T')[0].split('-')[0]
return `${day} ${month} ${year}`
}
which takes in the object and the name of the field i want to format like formatISODate(obj, ‘salesCertificationExpiry’)
So my question is:
- is there an easy way to do this (am i making it more complex than it has to be)
- i can’t seem to reference the field parameter (i get ‘obj.data[field].split’ is not a function) nor can i get the value of the field with the matching parameter.
If anyone can point me in the right direction, that’d be great, as i think it’s nearly there.
Thanks,
Jason
3
Answers
Find year, month and day with a regexp, reverse and join:
There’s no need to have 2 arguments, just pass the property:
You can also reduce the found parts:
Can try this with the code reusability !!