Date helpers
- Node.js course
- Intermediate
- Lesson 22/116
- 6 min
Add Date processing functions.
Date helpers
Now we add a date helper to the helpers object: formatDate. This method converts an ISO date string ("2024-03-15") into the "15.03.2024" format.
Example: helpers.formatDate = function(iso) { const [y, m, d] = iso.split("-"); return d + "." + m + "." + y; }. Here iso.split("-") splits the string at dashes and gives an array; [y, m, d] destructures it. The call helpers.formatDate("2024-03-15") returns 15.03.2024. This is a practical way to turn ISO dates into a displayable format. We take the previous lessons' solution and add this method.
Parsing with split
The split method splits a string at a given separator and gives an array. "2024-03-15".split("-") gives ["2024", "03", "15"]. With destructuring you assign each part to a separate variable.
Example: const [y, m, d] = iso.split("-");. Here y = "2024", m = "03", d = "15". Then you join the new order with d + "." + m + "." + y. Destructuring assigns the array's items to variables in order; the order matters. This technique is the basic way to pull data from structured strings like dates, times and CSV.
Practice Add the formatDate method and call it
Take the previous lessons' solution. Add the helpers.formatDate method with body const [y, m, d] = iso.split("-"); return d + "." + m + "." + y;. Print the result with console.log(helpers.formatDate("2024-03-15")).
Starter code
const helpers = {};
module.exports = helpers;
console.log("Ready");
helpers.capitalize = function(text) {
if (!text) return text;
return text[0].toUpperCase() + text.slice(1);
};
console.log(helpers.capitalize("hello"));
helpers.clamp = function(value, min, max) {
if (value < min) return min;
if (value > max) return max;
return value;
};
console.log(helpers.clamp(15, 0, 10));
// Add the formatDate method
// write here
// Call formatDate and print the result
// write hereSample solution
const helpers = {};
module.exports = helpers;
console.log("Ready");
helpers.capitalize = function(text) {
if (!text) return text;
return text[0].toUpperCase() + text.slice(1);
};
console.log(helpers.capitalize("hello"));
helpers.clamp = function(value, min, max) {
if (value < min) return min;
if (value > max) return max;
return value;
};
console.log(helpers.clamp(15, 0, 10));
helpers.formatDate = function(iso) {
const [y, m, d] = iso.split("-");
return d + "." + m + "." + y;
};
console.log(helpers.formatDate("2024-03-15"));Expected output
Hazır Merhaba 10 15.03.2024
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does formatDate("2024-03-15") return?
helpers.formatDate("2024-03-15")- "2024-03-15"
- "15.03.2024" Correct answer
- "2024.03.15"
- "03.15.2024"
Explanation: The format is day.month.year: 15.03.2024.
2 Match each piece to its role
| Term | Meaning |
|---|---|
split("-") |
Splits the string at dashes |
[y, m, d] |
Takes parts with destructuring |
d + "." + m + "." + y |
Joins the new order |
iso |
Input date string |
3 Complete the formatDate return
return ____ + "." + m + "." + y;
return d + "." + m + "." + y;
Explanation: Write return d + "." + m + "." + y;.