Appointment Time (Solution is not working)

Hello. I’m a total beginner in Javascript. My code works fine when I test it on different online compiler, but it always fails Test 3. The error mentions that “uses the passed in current time,” and the expected output is 2000 while mine is 2025.

This is my solution:

export function getAppointmentDetails(timestamp) {
  const dateObj = new Date(timestamp);
  return { 
    year: dateObj.getUTCFullYear(), 
    month: dateObj.getUTCMonth() + 1, 
    date: dateObj.getUTCDate(), 
    hour: dateObj.getUTCHours(), 
    minute: dateObj.getUTCMinutes()
  };
}

Here’s the test case for that task:

describe('getAppointment', () => {
  test('extracts appointment details', () => {
    expect(getAppointmentDetails('2022-04-24T08:15:00.000')).toStrictEqual({
      year: 2022,
      month: 3,
      date: 24,
      hour: 8,
      minute: 15,
    });
  });
});

Double check the values you are sending back.

Second question: what timezone is the dateObj situated in?

Can you show us the full error message in a codeblock for this? It sounds like it’s for a different task so it’d be useful to see all your code as well.

There’s an observation in the concept regarding default timezones when the timestamp is in ISO string format.

/**
 * Create an appointment
 *
 * @param {number} days
 * @param {number} [now] (ms since the epoch, or undefined)
 *
 * @returns {Date} the appointment
 */
export function createAppointment(days, now = undefined) {
  let baseDate;
  if (now instanceof Date && isNaN(now)) {
    baseDate = new Date(now);
  } else {
    baseDate = new Date();
  }
  baseDate.setDate(baseDate.getDate() + days);
  return baseDate;
}

/**

 * Generate the appointment timestamp
 *
 * @param {Date} appointmentDate
 *
 * @returns {string} timestamp
 */
export function getAppointmentTimestamp(appointmentDate) {
  return appointmentDate.toISOString();
}

/**
 * Get details of an appointment
 *
 * @param {string} timestamp (ISO 8601)
 *
 * @returns {Record<'year' | 'month' | 'date' | 'hour' | 'minute', number>} the appointment details
 */
export function getAppointmentDetails(timestamp) {
  const dateObj = new Date(timestamp);
  return { 
    year: 2020, 
    month: dateObj.getUTCMonth() + 1, 
    date: dateObj.getUTCDate(), 
    hour: dateObj.getUTCHours(), 
    minute: dateObj.getUTCMinutes()
  };
}

/**
 * Update an appointment with given options
 *
 * @param {string} timestamp (ISO 8601)
 * @param {Partial<Record<'year' | 'month' | 'date' | 'hour' | 'minute', number>>} options
 *
 * @returns {Record<'year' | 'month' | 'date' | 'hour' | 'minute', number>} the appointment details
 */
export function updateAppointment(timestamp, options) {
  let date = new Date(timestamp);
  let newAppointmentDate = {
    year: date.getFullYear(),
    month: date.getMonth(),
    date: date.getDate(),
    hour: date.getHours(),
    minute: date.getMinutes()
  }
  for (const key in newAppointmentDate) {
    if (key === Object.keys(options)) {
      newAppointmentDate[key] = options[key];
    }
  }
  return newAppointmentDate;
}

/**
 * Get available time in seconds (rounded) between two appointments
 *
 * @param {string} timestampA (ISO 8601)
 * @param {string} timestampB (ISO 8601)
 *
 * @returns {number} amount of seconds (rounded)
 */
export function timeBetween(timestampA, timestampB) {
  return Math.abs(new Date(timestampA) - new Date(timestampB)) / 1000;
}

/**
 * Get available times between two appointment
 *
 * @param {string} appointmentTimestamp (ISO 8601)
 * @param {string} currentTimestamp (ISO 8601)
 */
export function isValid(appointmentTimestamp, currentTimestamp) {
  return new Date(appointmentTimestamp) > new Date(currentTimestamp);
}

OK, I found multiple issues here - with the tests and the OPs code.

First of all, the failing test is test 3 uses the passed in current time of task 1 (not Task 3). The result is shown in the Online editor as:

Code Run

expect(received).toEqual(expected) // deep equality

Test Failure

Expected: 2000
Received: 2025

That’s issue 2: Not a very helpful code snippet. What are the inputs? What method is actually called how?

Then, issue 3, as things are no longer executed in Exercisms infrastructure but the local Browser, there are many hard to understand timezone problems. I had to use getUTCDate() / setUTCDate() to make the OPs code actually get to where the OP received the initially posted test failure.

Last issue with the tests: The tests name uses the passed in current time is misleading: It passes in a UNIX timestamp far in the past, not a current time. Together with not seeing the inputs, it is impossible to understand that the current year (2025 as the OP got it in the failure message) is actually the wrong one, not 2000.

The issue making the OPs code fail at test 3 is rather “as expected”. The type checking of the input now does not work as the OP wanted it to work in createAppointment(). So for test 3 the initial Date object is made without using argument now.