--- title: Date manipulation id: 5966c21cf732a95f1b67dd28 challengeType: 5 --- ## Description
Task:

Given a date string in EST, output the given date as a string with 12 hours added to the time.

Time zone should be preserved.

Example input:

"March 7 2009 7:30pm EST"

Example output:

"March 8 2009 7:30am EST"

## Instructions
## Tests
```yml tests: - text: add12Hours is a function. testString: 'assert(typeof add12Hours === "function", "add12Hours is a function.");' - text: add12Hours(dateString) should return a string. testString: 'assert(typeof add12Hours(tests[0]) === "string", "add12Hours(dateString) should return a string.");' - text: 'add12Hours("" + tests[0] + "") should return "" + answers[0] + ""' testString: 'assert(add12Hours(tests[0]) === answers[0], "add12Hours("" + tests[0] + "") should return "" + answers[0] + """);' - text: 'Should handel day change. add12Hours("" + tests[1] + "") should return "" + answers[1] + ""' testString: 'assert(add12Hours(tests[1]) === answers[1], "Should handel day change. add12Hours("" + tests[1] + "") should return "" + answers[1] + """);' - text: 'Should handel month change in a leap years. add12Hours("" + tests[2] + "") should return "" + answers[2] + ""' testString: 'assert(add12Hours(tests[2]) === answers[2], "Should handel month change in a leap years. add12Hours("" + tests[2] + "") should return "" + answers[2] + """);' - text: 'Should handel month change in a common years. add12Hours("" + tests[3] + "") should return "" + answers[3] + ""' testString: 'assert(add12Hours(tests[3]) === answers[3], "Should handel month change in a common years. add12Hours("" + tests[3] + "") should return "" + answers[3] + """);' - text: 'Should handel year change. add12Hours("" + tests[4] + "") should return "" + answers[4] + ""' testString: 'assert(add12Hours(tests[4]) === answers[4], "Should handel year change. add12Hours("" + tests[4] + "") should return "" + answers[4] + """);' ```
## Challenge Seed
```js function add12Hours (dateString) { // Good luck! return true; } ```
### After Test
```js console.info('after the test'); ```
## Solution
```js function add12Hours (dateString) { const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; // Get the parts of the string const parts = dateString.split(' '); const month = months.indexOf(parts[0]); const day = parseInt(parts[1], 10); const year = parseInt(parts[2], 10); const time = parts[3].split(':'); let hours = parseInt(time[0], 10); if (time[1].slice(-2) === 'pm') { hours += 12; } const minutes = parseInt(time[1].slice(0, -2), 10); // Create a date with given parts, and updated hours const date = new Date(); date.setFullYear(year, month, day); date.setHours(hours + 12); // Add 12 hours date.setMinutes(minutes); let hoursOutput = date.getHours(); let abbreviation = 'am'; if (hoursOutput > 12) { hoursOutput -= 12; abbreviation = 'pm'; } return `${months[date.getMonth()]} ${date.getDate()} ${date.getFullYear()} ${hoursOutput}:${date.getMinutes()}${abbreviation} EST`; } ```