curriculum/challenges/english/blocks/lab-date-conversion/66f686b8ebdb982fa8e14330.md
In this lab, you will code a date conversion program that converts a given date to different formats. For example, the current date Fri Sep 27 2024 16:04:43 GMT+0500 (Pakistan Standard Time) would be formatted in the following 2 ways:
(MM/DD/YYYY): 9/27/2024.(Month Day, Year): September 27, 2024.Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories:
currentDate and assign it the current date and time using the Date object.currentDateFormat and assign it the string Current Date and Time: [current date]. Replace [current date] with the result of the currentDate variable.currentDateFormat to the console.formatDateMMDDYYYY that takes the date object as a parameter. You can name this parameter whatever you like.formatDateMMDDYYYY function should convert the current date to month/day/year format and return the string Formatted Date (MM/DD/YYYY): [month]/[day]/[year].formatDateLong that takes the date object as a parameter.formatDateLong function should convert the current date to Month Day, Year format and return the string Formatted Date (Month Day, Year): [formatted date].Note: For the tests to pass, make sure that you use en-US for the locale when formatting the dates.
You should have a variable named currentDate that holds the current date and time using the Date object.
// excluded timestamps
const expectedDate = new Date();
const expectedDateOnly = new Date(expectedDate.getFullYear(), expectedDate.getMonth(), expectedDate.getDate());
const currentDateOnly = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate());
assert.equal(currentDateOnly.toString(), expectedDateOnly.toString());
You should have a variable named currentDateFormat that holds the current date in the format Current Date and Time: <ddd> <MMM> <dd> <yyyy> <HH>:<mm>:<ss> <TIMEZONE>.
const expectedDateString = `Current Date and Time: `;
assert.include(currentDateFormat, expectedDateString);
const currentTimestamp = new Date(currentDateFormat.replace(expectedDateString, '')).getTime();
const expectedTimestamp = new Date().getTime();
assert.approximately(currentTimestamp, expectedTimestamp, 2000);
You should log the value of currentDateFormat to the console.
assert.match(code, /console\.log\(\s*currentDateFormat\s*\)/);
You should have a function formatDateMMDDYYYY
assert.isFunction(formatDateMMDDYYYY);
The function formatDateMMDDYYYY should take a single parameter.
assert.lengthOf(formatDateMMDDYYYY, 1);
When the date object holds Fri Sep 27 2024 16:16:11 GMT+0500 (Pakistan Standard Time), the function formatDateMMDDYYYY should return Formatted Date (MM/DD/YYYY): 9/27/2024.
let dateString = "Fri Sep 27 2024 16:21:18 GMT+0500 (Pakistan Standard Time)";
let dateObject = new Date(dateString);
assert.equal(formatDateMMDDYYYY(dateObject).toString(), "Formatted Date (MM/DD/YYYY): 9/27/2024");
// prevent hardcoding
dateString = "Fri Sep 28 2024 18:31:18 GMT+0500 (Pakistan Standard Time)";
dateObject = new Date(dateString);
assert.strictEqual(formatDateMMDDYYYY(dateObject).toString(), "Formatted Date (MM/DD/YYYY): 9/28/2024");
You should have a function formatDateLong
assert.isFunction(formatDateLong);
The function formatDateLong should take a single a parameter.
assert.lengthOf(formatDateLong, 1);
When the date object holds Fri Sep 27 2024 16:16:11 GMT+0500 (Pakistan Standard Time), the function formatDateLong should return Formatted Date (Month Day, Year): September 27, 2024.
let testDate = new Date('Fri Sep 27 2024 16:21:18 GMT+0500 (Pakistan Standard Time)');
let result = formatDateLong(testDate);
assert.strictEqual(result,'Formatted Date (Month Day, Year): September 27, 2024', `Test failed: ${result}`);
// prevent hardcoding
testDate = new Date('Fri Sep 28 2024 18:31:18 GMT+0500 (Pakistan Standard Time)');
result = formatDateLong(testDate);
assert.strictEqual(result, 'Formatted Date (Month Day, Year): September 28, 2024');
const currentDate = new Date();
const currentDateFormat = `Current Date and Time: ${currentDate}`;
console.log(currentDateFormat);
function formatDateMMDDYYYY(date) {
const month = date.getMonth() + 1;
const day = date.getDate();
const year = date.getFullYear();
return `Formatted Date (MM/DD/YYYY): ${month}/${day}/${year}`;
}
function formatDateLong(date) {
const longDateFormat = date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
return `Formatted Date (Month Day, Year): ${longDateFormat}`;
}
// usage
console.log(formatDateMMDDYYYY(currentDate));
console.log(formatDateLong(currentDate));