freeCodeCamp/curriculum/challenges/chinese/10-coding-interview-prep/rosetta-code/date-format.md

85 lines
2.1 KiB
Markdown

---
id: 59669d08d75b60482359409f
title: Date format
challengeType: 5
forumTopicId: 302243
dashedName: date-format
---
# --description--
Return an array with the current date in the formats:
<ul>
<li>2007-11-23</li>
<li>Friday, November 23, 2007</li>
</ul>
Example output: `['2007-11-23', 'Friday, November 23, 2007']`
# --hints--
`getDateFormats` should be a function.
```js
assert(typeof getDateFormats === 'function');
```
`getDateFormats` should return an object.
```js
assert(typeof getDateFormats() === 'object');
```
`getDateFormats` should return an array with 2 elements.
```js
assert(getDateFormats().length === 2);
```
`getDateFormats` should return the correct date in the right format
```js
assert.deepEqual(getDateFormats(), dates, equalsMessage);
```
# --seed--
## --after-user-code--
```js
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>.`;
```
## --seed-contents--
```js
function getDateFormats() {
return true;
}
```
# --solutions--
```js
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];
}
```