Age Calculator App
A frontend development challenge that calculates age based on day, month, and year inputs.
Challenge provided by Frontend Mentor.
Skills
- Frontend Development
- Accessibility Compliance
- User Interface Design
Timeline
August 2023
Preface
This is a solution to the Age calculator app challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.
The Challenge
Users should be able to:
- View an age in years, months, and days after submitting a valid date through the form
- Receive validation errors if:
- Any field is empty when the form is submitted
- The day number is not between 1-31
- The month number is not between 1-12
- The year is in the future
- The date is invalid e.g. 31/04/1991 (there are 30 days in April)
- View the optimal layout for the interface depending on their device's screen size
- See hover and focus states for all interactive elements on the page
Learnings
Having seperate input fields for date management turned out to be pretty tricky for validation. My initial structure looks clean, but resulted in a lot of overhead. Using `date-fns` helped with utilities and formatting.
const date = new Date();
const currentDay = date.getUTCDate();
const currentMonth = date.getUTCMonth() + 1;
const currentYear = date.getUTCFullYear();
const [isDay, setDay] = useState();
const [isMonth, setMonth] = useState();
const [isYear, setYear] = useState();
const compareCurrentDate = date;
let compareSubmittedDate = new Date(
currentYear,
currentMonth - 1,
currentDay
);
if (isYear && isMonth && isDay) {
compareSubmittedDate = new Date(isYear, isMonth - 1, isDay);
}
const dateDuration = intervalToDuration({
start: compareCurrentDate,
end: compareSubmittedDate,
});Resources
- Regex Example: This helped structure the expected values for each input field.
- Validation Example: This helped structure the individual error messages for each input.
