skip to Main Content

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:

  1. is there an easy way to do this (am i making it more complex than it has to be)
  2. 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


  1. const formatISODate = function (obj, field) {
      let day = obj[field].split('T')[0].split('-')[2]
      let month = obj[field].split('T')[0].split('-')[1]
      let year = obj[field].split('T')[0].split('-')[0]
    
      return `${day} ${month} ${year}`
    }
    Login or Signup to reply.
  2. Find year, month and day with a regexp, reverse and join:

    const formatISODate = (obj, field) => obj[field].match(/(d{4})-(d{2})-(d{2})/).slice(1).reverse().join(' ');
    
    console.log(formatISODate(obj, 'techicalCertificationExpiry'));
    <script>
    const obj = {
      "_id": "63dd35e801ad6ada9a5eeb08",
      "name": "Joe Bloggs",
      "technicalCertification": true,
      "salesCertification": true,
      "salesCertificationExpiry": "2023-12-27T12:02:31.043Z",
      "techicalCertificationExpiry": "2024-09-29T12:02:48.254Z"
    };
    </script>

    There’s no need to have 2 arguments, just pass the property:

    const formatISODate = str => str.match(/(d{4})-(d{2})-(d{2})/).slice(1).reverse().join(' ');
    
    console.log(formatISODate(obj.techicalCertificationExpiry));
    <script>
    const obj = {
      "_id": "63dd35e801ad6ada9a5eeb08",
      "name": "Joe Bloggs",
      "technicalCertification": true,
      "salesCertification": true,
      "salesCertificationExpiry": "2023-12-27T12:02:31.043Z",
      "techicalCertificationExpiry": "2024-09-29T12:02:48.254Z"
    };
    </script>

    You can also reduce the found parts:

    const formatISODate = str => str.match(/(d{4})-(d{2})-(d{2})/).slice(1).reduceRight((r, p) => r + ' ' + p);
    
    console.log(formatISODate(obj.techicalCertificationExpiry));
    <script>
    const obj = {
      "_id": "63dd35e801ad6ada9a5eeb08",
      "name": "Joe Bloggs",
      "technicalCertification": true,
      "salesCertification": true,
      "salesCertificationExpiry": "2023-12-27T12:02:31.043Z",
      "techicalCertificationExpiry": "2024-09-29T12:02:48.254Z"
    };
    </script>
    Login or Signup to reply.
  3. const formatISODate = function (obj, field) {
      let baseDate = obj[field].split('T')[0].split('-');
      return `${baseDate[2]} ${baseDate[1]} ${baseDate[0]}`;
    }
    

    Can try this with the code reusability !!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search