freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/date-format.english.md

2.5 KiB

title id challengeType
Date format 59669d08d75b60482359409f 5

Description

Return an array with the current date in the formats:
  • 2007-11-23 and
  • Sunday, November 23, 2007
Example output: ['2007-11-23', 'Sunday, November 23, 2007']

Instructions

Tests

tests:
  - text: <code>getDateFormats</code> is a function.
    testString: assert(typeof getDateFormats === 'function', '<code>getDateFormats</code> is a function.');
  - text: Should return an object.
    testString: assert(typeof getDateFormats() === 'object', 'Should return an object.');
  - text: Should returned an array with 2 elements.
    testString: assert(getDateFormats().length === 2, 'Should returned an array with 2 elements.');
  - text: Should return the correct date in the right format
    testString: assert.deepEqual(getDateFormats(), dates, equalsMessage);

Challenge Seed

function getDateFormats() {
  // Good luck!
  return true;
}

After Test

const getDateSolution = () => {
  const date = new Date();
  const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  const fmt1 = `${date.getFullYear()}-${(1 + date.getMonth())}-${date.getDate()}`;
  const fmt2 = `${weekdays[date.getDay()]}, ${months[date.getMonth()]} ${date.getDate()}, ${date.getFullYear()}`;
  return [fmt1, fmt2];
};

const dates = getDateSolution();
const equalsMessage = `message: <code>getDataFormats()</code> should return <code>["${dates[0]}", "${dates[1]}"]</code>.`;

Solution

function getDateFormats() {
  const date = new Date();
  const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  const fmt1 = `${date.getFullYear()}-${(1 + date.getMonth())}-${date.getDate()}`;
  const fmt2 = `${weekdays[date.getDay()]}, ${months[date.getMonth()]} ${date.getDate()}, ${date.getFullYear()}`;
  return [fmt1, fmt2];
}