1

so I have a list of array of time and date I want to join both appointmateDate and appointmentTime to iso format and get a new array of timeRange is that possible🙏🏻

const time = [
            {
                "appointmentDate": "2021-12-24T23:00:00.000Z",
                "appointmentTime": "17:51 am"
            },
            {
                "appointmentDate": "2021-12-24T23:00:00.000Z",
                "appointmentTime": "18:51 am"
            },
            {
                "appointmentDate": "2021-12-24T23:00:00.000Z",
                "appointmentTime": "19:51 am"
            },
            {
                "appointmentDate": "2021-12-24T23:00:00.000Z",
                "appointmentTime": "20:51 am"
            }
        ]

         console.log(time)

2 Answers 2

2

Using setHours.

Loop over the array using Array#map and create a new Date object using appointmentDate and then using setHours and appointmentTime set the time.

NOTE: 20:51 am is not a valid time, if it is in 24 hour format there's no need for am, pm.

const 
  time = [{appointmentDate:"2021-12-24T23:00:00.000Z",appointmentTime:"17:51 am"},{appointmentDate:"2021-12-24T23:00:00.000Z",appointmentTime:"18:51 am"},{appointmentDate:"2021-12-24T23:00:00.000Z",appointmentTime:"19:51 am"},{appointmentDate:"2021-12-24T23:00:00.000Z",appointmentTime:"20:51 am"}],

  res = time.map(({ appointmentDate, appointmentTime }) => {
    const date = new Date(appointmentDate);
    const hour = appointmentTime.slice(0, 2);
    const min = appointmentTime.slice(3, 5);
    date.setHours(hour, min)
    return date.toISOString();
  });

console.log(res);

One Liner

Logic remains exactly the same, its just expressions instead of statements.

const 
  time = [{appointmentDate:"2021-12-24T23:00:00.000Z",appointmentTime:"17:51 am"},{appointmentDate:"2021-12-24T23:00:00.000Z",appointmentTime:"18:51 am"},{appointmentDate:"2021-12-24T23:00:00.000Z",appointmentTime:"19:51 am"},{appointmentDate:"2021-12-24T23:00:00.000Z",appointmentTime:"20:51 am"}],
  
  res = time.map(
    ({ appointmentDate, appointmentTime }, _i, _arr, d = new Date(appointmentDate)) => 
    (d.setHours(appointmentTime.slice(0, 2), appointmentTime.slice(3, 5)), d.toISOString())
  );

console.log(res);

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

Comments

2

What wasn't super clear is how that time portion was expected to work. As in, 17:51 am isn't a valid hour:minute time fragment since am indicates a 12 hour clock, but 17 indicates as 24 hour clock. But then I noticed all of your date strings included a 23 hour hold, so if that time portion is just the minute:second value. If that's the case, the below bit should work.

However, this still doesn't account for Timezones (indicated by the Z in ISO format). Also, it's not really clear what you mean by "ranges" as these are just individual date strings -- not relation to one another.

I recommend clarifying your question. Anyway, hope this helpful:

const time = [
  {
    appointmentDate: '2021-12-24T23:00:00.000Z',
    appointmentTime: '17:51 am',
  },
  {
    appointmentDate: '2021-12-24T23:00:00.000Z',
    appointmentTime: '18:51 am',
  },
  {
    appointmentDate: '2021-12-24T23:00:00.000Z',
    appointmentTime: '19:51 am',
  },
  {
    appointmentDate: '2021-12-24T23:00:00.000Z',
    appointmentTime: '20:51 am',
  },
]

time.map(obj => {
  let newTimeString = obj.appointmentTime.substr(0, 5)
  return obj.appointmentDate.replace('00:00.000Z', newTimeString + '.000Z')
})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.