├── README.md ├── assets ├── css │ ├── style.css │ └── test.txt ├── fonts │ ├── OFL.txt │ ├── Poppins-Bold.ttf │ ├── Poppins-BoldItalic.ttf │ ├── Poppins-ExtraBold.ttf │ ├── Poppins-ExtraBoldItalic.ttf │ ├── Poppins-Italic.ttf │ └── Poppins-Regular.ttf ├── images │ ├── favicon-32x32.png │ └── icon-arrow.svg └── js │ └── main.js ├── design ├── active-states.jpg ├── desktop-completed.jpg ├── desktop-design.jpg ├── desktop-error-empty.jpg ├── desktop-error-invalid.jpg ├── desktop-error-whole-form.jpg ├── desktop-preview.jpg ├── mobile-design.jpg └── test.txt └── index.html /README.md: -------------------------------------------------------------------------------- 1 | **Creating a Year Calculator App with HTML, CSS, and JavaScript** 2 | 3 | 4 | Introducing the AgeCraft App! Ever wondered about the precise duration of your existence? Dive into this tutorial where we'll guide you to craft a sleek web application that instantly calculates your age in years, months, days, hours, and minutes. Unleash your coding prowess and let's make time tangible! 🚀 #AgeCraft #WebDevMagic 5 | 6 | ## **HTML Structure** 7 | 8 | We start by setting up the basic structure of our app using HTML. Save the following code in an `index.html` file: 9 | 10 | ```html 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 |
21 | 22 | 23 | 24 |
25 |
26 | 29 | 30 | 31 | 32 | 33 | ``` 34 | 35 | Customized Styling with CSS 36 | 37 | Let's add a touch of uniqueness to our app by applying some creative CSS styles. Open the style.css file and replace the placeholder with the following code: 38 | 39 | /* style.css */ 40 | 41 | body { 42 | font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 43 | background-color: #f5f5f5; 44 | margin: 0; 45 | display: flex; 46 | justify-content: center; 47 | align-items: center; 48 | height: 100vh; 49 | } 50 | 51 | .container { 52 | background-color: #fff; 53 | border-radius: 10px; 54 | box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); 55 | padding: 20px; 56 | text-align: center; 57 | } 58 | 59 | h1 { 60 | color: #333; 61 | } 62 | 63 | input { 64 | padding: 10px; 65 | margin: 10px; 66 | border: 1px solid #ccc; 67 | border-radius: 5px; 68 | } 69 | 70 | button { 71 | padding: 10px 20px; 72 | background-color: #4caf50; 73 | color: #fff; 74 | border: none; 75 | border-radius: 5px; 76 | cursor: pointer; 77 | transition: background-color 0.3s ease; 78 | } 79 | 80 | button:hover { 81 | background-color: #45a049; 82 | } 83 | 84 | Enhanced Functionality with JavaScript 85 | 86 | Let's bring more life to our app by refining the JavaScript functionality. Open the main.js file and replace the placeholder with the following code: 87 | 88 | // main.js 89 | 90 | function calculateAge() { 91 | var birthdate = new Date(document.getElementById('birthdate').value); 92 | var currentDate = new Date(); 93 | 94 | var ageInMilliseconds = currentDate - birthdate; 95 | var ageInSeconds = ageInMilliseconds / 1000; 96 | 97 | var years = Math.floor(ageInSeconds / (365.25 * 24 * 60 * 60)); 98 | var months = Math.floor((ageInSeconds % (365.25 * 24 * 60 * 60)) / (30.44 * 24 * 60 * 60)); 99 | var days = Math.floor((ageInSeconds % (30.44 * 24 * 60 * 60)) / (24 * 60 * 60)); 100 | var hours = Math.floor((ageInSeconds % (24 * 60 * 60)) / (60 * 60)); 101 | var minutes = Math.floor((ageInSeconds % (60 * 60)) / 60); 102 | 103 | document.getElementById('result').innerHTML = ` 104 |

Years: ${years}

105 |

Months: ${months}

106 |

Days: ${days}

107 |

Hours: ${hours}

108 |

Minutes: ${minutes}

109 | `; 110 | } 111 | 112 | Complete App Integration 113 | 114 | Replace the placeholders in your HTML file with the actual code snippets. Your users will now experience an interactive and uniquely styled Year Calculator App. 115 | 116 | Feel free to explore further customization options, add animations, or incorporate additional features to make the app even more engaging. The world of web development is vast, and this project is just the beginning of what you can create. 117 | Enjoy coding! ☕️ -------------------------------------------------------------------------------- /assets/css/style.css: -------------------------------------------------------------------------------- 1 | @import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,700;1,400;1,800&display=swap'); 2 | 3 | :root { 4 | --Purple: hsl(20, 100%, 65%); 5 | --Light-red: hsl(0, 100%, 67%); 6 | --White: hsl(0, 69%, 77%); 7 | --Off-white: hsl(0, 95%, 49%); 8 | --Light-grey: hsl(0, 5%, 11%); 9 | --Smokey-grey: hsl(0, 74%, 48%); 10 | --Off-black: hsl(0, 94%, 49%); 11 | } 12 | 13 | * { 14 | margin: 0; 15 | padding: 0; 16 | box-sizing: border-box; 17 | font-family: 'Poppins', sans-serif; 18 | } 19 | 20 | body { 21 | background: var(--Light-grey); 22 | } 23 | 24 | .container { 25 | display: flex; 26 | justify-content: center; 27 | align-items: center; 28 | height: 100vh; 29 | } 30 | 31 | .content { 32 | background: var(--White); 33 | width: 750px; 34 | padding: 50px; 35 | border-radius: 20px; 36 | border-bottom-right-radius: 40%; 37 | position: relative; 38 | } 39 | 40 | .input { 41 | display: flex; 42 | margin-bottom: 35px; 43 | } 44 | 45 | input { 46 | font-size: 32px; 47 | width: 150px; 48 | padding: 10px; 49 | margin-top: 10px; 50 | margin-right: 20px; 51 | border-radius: 10px; 52 | border: 0.5px solid var(--Light-grey); 53 | outline: none; 54 | letter-spacing: 2px; 55 | caret-color: var(--Purple); 56 | } 57 | 58 | h6 { 59 | text-transform: uppercase; 60 | letter-spacing: 2px; 61 | color: var(--Light-red); 62 | } 63 | 64 | hr { 65 | border: none; 66 | height: 1px; 67 | background: var(--Light-grey); 68 | } 69 | 70 | button { 71 | position: absolute; 72 | width: 65px; 73 | height: 65px; 74 | border-radius: 50%; 75 | border: none; 76 | outline: none; 77 | background: var(--Purple); 78 | right: 50px; 79 | margin-top: -32px; 80 | cursor: pointer; 81 | } 82 | 83 | button:hover { 84 | background: var(--Off-black); 85 | } 86 | 87 | input:focus { 88 | border: 0.5px solid var(--Purple); 89 | } 90 | 91 | p.error { 92 | font-size: 12px; 93 | font-weight: 400; 94 | color: var(--Light-red); 95 | font-style: italic; 96 | margin: 5px 0; 97 | } 98 | 99 | .result h1 { 100 | font-size: 60px; 101 | font-style: italic; 102 | } 103 | 104 | .result span { 105 | color: var(--Purple); 106 | } 107 | 108 | .attribution { 109 | font-size: 11px; 110 | text-align: center; 111 | } 112 | 113 | .attribution a { 114 | color: hsl(228, 45%, 44%); 115 | } 116 | 117 | @media only screen and (max-width: 375px) { 118 | .input { 119 | margin-bottom: 40px; 120 | } 121 | input { 122 | font-size: 18px; 123 | width: 80px; 124 | margin-right: 10px; 125 | border-radius: 5px; 126 | } 127 | .content { 128 | background: var(--White); 129 | width: 300px; 130 | padding: 20px; 131 | border-radius: 10px; 132 | border-bottom-right-radius: 30%; 133 | position: relative; 134 | } 135 | .result h1 { 136 | font-size: 45px; 137 | font-style: italic; 138 | } 139 | p.error { 140 | font-size: 10px; 141 | } 142 | button { 143 | right: 40%; 144 | } 145 | .result { 146 | margin-top: 40px; 147 | } 148 | } -------------------------------------------------------------------------------- /assets/css/test.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /assets/fonts/OFL.txt: -------------------------------------------------------------------------------- 1 | Copyright 2020 The Poppins Project Authors (https://github.com/itfoundry/Poppins) 2 | 3 | This Font Software is licensed under the SIL Open Font License, Version 1.1. 4 | This license is copied below, and is also available with a FAQ at: 5 | http://scripts.sil.org/OFL 6 | 7 | 8 | ----------------------------------------------------------- 9 | SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 10 | ----------------------------------------------------------- 11 | 12 | PREAMBLE 13 | The goals of the Open Font License (OFL) are to stimulate worldwide 14 | development of collaborative font projects, to support the font creation 15 | efforts of academic and linguistic communities, and to provide a free and 16 | open framework in which fonts may be shared and improved in partnership 17 | with others. 18 | 19 | The OFL allows the licensed fonts to be used, studied, modified and 20 | redistributed freely as long as they are not sold by themselves. The 21 | fonts, including any derivative works, can be bundled, embedded, 22 | redistributed and/or sold with any software provided that any reserved 23 | names are not used by derivative works. The fonts and derivatives, 24 | however, cannot be released under any other type of license. The 25 | requirement for fonts to remain under this license does not apply 26 | to any document created using the fonts or their derivatives. 27 | 28 | DEFINITIONS 29 | "Font Software" refers to the set of files released by the Copyright 30 | Holder(s) under this license and clearly marked as such. This may 31 | include source files, build scripts and documentation. 32 | 33 | "Reserved Font Name" refers to any names specified as such after the 34 | copyright statement(s). 35 | 36 | "Original Version" refers to the collection of Font Software components as 37 | distributed by the Copyright Holder(s). 38 | 39 | "Modified Version" refers to any derivative made by adding to, deleting, 40 | or substituting -- in part or in whole -- any of the components of the 41 | Original Version, by changing formats or by porting the Font Software to a 42 | new environment. 43 | 44 | "Author" refers to any designer, engineer, programmer, technical 45 | writer or other person who contributed to the Font Software. 46 | 47 | PERMISSION & CONDITIONS 48 | Permission is hereby granted, free of charge, to any person obtaining 49 | a copy of the Font Software, to use, study, copy, merge, embed, modify, 50 | redistribute, and sell modified and unmodified copies of the Font 51 | Software, subject to the following conditions: 52 | 53 | 1) Neither the Font Software nor any of its individual components, 54 | in Original or Modified Versions, may be sold by itself. 55 | 56 | 2) Original or Modified Versions of the Font Software may be bundled, 57 | redistributed and/or sold with any software, provided that each copy 58 | contains the above copyright notice and this license. These can be 59 | included either as stand-alone text files, human-readable headers or 60 | in the appropriate machine-readable metadata fields within text or 61 | binary files as long as those fields can be easily viewed by the user. 62 | 63 | 3) No Modified Version of the Font Software may use the Reserved Font 64 | Name(s) unless explicit written permission is granted by the corresponding 65 | Copyright Holder. This restriction only applies to the primary font name as 66 | presented to the users. 67 | 68 | 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font 69 | Software shall not be used to promote, endorse or advertise any 70 | Modified Version, except to acknowledge the contribution(s) of the 71 | Copyright Holder(s) and the Author(s) or with their explicit written 72 | permission. 73 | 74 | 5) The Font Software, modified or unmodified, in part or in whole, 75 | must be distributed entirely under this license, and must not be 76 | distributed under any other license. The requirement for fonts to 77 | remain under this license does not apply to any document created 78 | using the Font Software. 79 | 80 | TERMINATION 81 | This license becomes null and void if any of the above conditions are 82 | not met. 83 | 84 | DISCLAIMER 85 | THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 86 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF 87 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 88 | OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE 89 | COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 90 | INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL 91 | DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 92 | FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM 93 | OTHER DEALINGS IN THE FONT SOFTWARE. 94 | -------------------------------------------------------------------------------- /assets/fonts/Poppins-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/assets/fonts/Poppins-Bold.ttf -------------------------------------------------------------------------------- /assets/fonts/Poppins-BoldItalic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/assets/fonts/Poppins-BoldItalic.ttf -------------------------------------------------------------------------------- /assets/fonts/Poppins-ExtraBold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/assets/fonts/Poppins-ExtraBold.ttf -------------------------------------------------------------------------------- /assets/fonts/Poppins-ExtraBoldItalic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/assets/fonts/Poppins-ExtraBoldItalic.ttf -------------------------------------------------------------------------------- /assets/fonts/Poppins-Italic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/assets/fonts/Poppins-Italic.ttf -------------------------------------------------------------------------------- /assets/fonts/Poppins-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/assets/fonts/Poppins-Regular.ttf -------------------------------------------------------------------------------- /assets/images/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/assets/images/favicon-32x32.png -------------------------------------------------------------------------------- /assets/images/icon-arrow.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /assets/js/main.js: -------------------------------------------------------------------------------- 1 | // Elements 2 | const dayIn = document.getElementById('dayIn'); 3 | const monthIn = document.getElementById('monthIn'); 4 | const yearIn = document.getElementById('yearIn'); 5 | const dayOut = document.getElementById('dayOut'); 6 | const monthOut = document.getElementById('monthOut'); 7 | const yearOut = document.getElementById('yearOut'); 8 | const calculateBtn = document.getElementById('calculateBtn'); 9 | const errorStyle = '0.5px solid var(--Light-red)'; 10 | 11 | // Calculate Button 12 | calculateBtn.addEventListener('click', () => { 13 | const D = dayIn.value; 14 | const M = monthIn.value; 15 | const Y = yearIn.value; 16 | const birthday = `${Y}-${M}-${D}`; 17 | 18 | if (validateDay() && validateMonth() && validateYear()) { 19 | console.log('Done'); 20 | } else { 21 | return; 22 | } 23 | 24 | const birthDate = new Date(birthday); 25 | const currentDate = new Date(); 26 | 27 | console.log(birthDate.getTime()); 28 | 29 | const timeDiff = currentDate.getTime() - birthDate.getTime(); 30 | 31 | // Age Calculation 32 | let years = new Date().getFullYear() - new Date(birthday).getFullYear(); 33 | let months = new Date().getMonth() - new Date(birthday).getMonth(); 34 | let days = new Date().getDate() - Number(D); 35 | const hours = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); 36 | const minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60)); 37 | 38 | // Display Values 39 | 40 | 41 | if (months < 0) { 42 | years = years - 1; 43 | months = months + 12; 44 | } 45 | 46 | if (days < 0) { 47 | days += getNoOfDays(Y, M - 1); 48 | } 49 | 50 | // Display Values 51 | dayOut.innerText = days; 52 | monthOut.innerText = months; 53 | yearOut.innerText = years; 54 | document.getElementById('hourOut').innerText = hours; 55 | document.getElementById('minuteOut').innerText = minutes; 56 | }); 57 | 58 | // Get Number of Days in a particular months 59 | function getNoOfDays(y, m) { 60 | return new Date(y, m, 0).getDate(); 61 | } 62 | 63 | /*================ on Blur Validation =========================*/ 64 | 65 | // On Blur day validation 66 | dayIn.addEventListener('blur', () => { 67 | validateDay(); 68 | }); 69 | 70 | // Validate Day function 71 | const validateDay = () => { 72 | const D = dayIn.value; 73 | const M = monthIn.value; 74 | const Y = yearIn.value; 75 | if (D == '') { 76 | showMessage(dayIn, 'This field is required', errorStyle); 77 | return false; 78 | } else if (!validDay(Y, M, D)) { 79 | showMessage(dayIn, 'Must be a valid day', errorStyle); 80 | return false; 81 | } else { 82 | showMessage(dayIn, '', ''); 83 | return true; 84 | } 85 | }; 86 | 87 | // On Blur month validation 88 | monthIn.addEventListener('blur', () => { 89 | validateMonth(); 90 | }); 91 | 92 | const validateMonth = () => { 93 | const M = monthIn.value; 94 | if (M == '') { 95 | showMessage(monthIn, 'This field is required', errorStyle); 96 | return false; 97 | } else if (!validMonth(M)) { 98 | showMessage(monthIn, 'Must be a valid month', errorStyle); 99 | return false; 100 | } else { 101 | showMessage(monthIn, '', ''); 102 | return true; 103 | } 104 | }; 105 | 106 | // on Blur Year validate 107 | yearIn.addEventListener('blur', () => { 108 | validateYear(); 109 | }); 110 | 111 | const validateYear = () => { 112 | const Y = yearIn.value; 113 | const M = monthIn.value; 114 | const D = dayIn.value; 115 | if (Y == '') { 116 | showMessage(yearIn, 'This field is required', errorStyle); 117 | return false; 118 | } else if (!validYear(Y, M, D)) { 119 | showMessage(yearIn, 'Must be in past', errorStyle); 120 | return false; 121 | } else { 122 | showMessage(yearIn, '', ''); 123 | return true; 124 | } 125 | }; 126 | 127 | // Validate Day 128 | function validDay(y, m, d) { 129 | if (d > getNoOfDays(y, m) || d < 1) return false; 130 | return true; 131 | } 132 | 133 | // validate Month 134 | function validMonth(m) { 135 | if (m > 12 || m < 1) return false; 136 | return true; 137 | } 138 | 139 | // Validate Year 140 | function validYear(y, m, d) { 141 | const secondDate = new Date(); 142 | const firstDate = new Date(`${y}-${m}-${d}`); 143 | if (firstDate.setHours(0, 0, 0, 0) <= secondDate.setHours(0, 0, 0, 0)) { 144 | return true; 145 | } 146 | return false; 147 | } 148 | 149 | // Display Message 150 | function showMessage(elem, msg, border) { 151 | elem.style.border = border; 152 | elem.nextElementSibling.innerText = msg; 153 | } 154 | -------------------------------------------------------------------------------- /design/active-states.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/design/active-states.jpg -------------------------------------------------------------------------------- /design/desktop-completed.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/design/desktop-completed.jpg -------------------------------------------------------------------------------- /design/desktop-design.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/design/desktop-design.jpg -------------------------------------------------------------------------------- /design/desktop-error-empty.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/design/desktop-error-empty.jpg -------------------------------------------------------------------------------- /design/desktop-error-invalid.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/design/desktop-error-invalid.jpg -------------------------------------------------------------------------------- /design/desktop-error-whole-form.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/design/desktop-error-whole-form.jpg -------------------------------------------------------------------------------- /design/desktop-preview.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/design/desktop-preview.jpg -------------------------------------------------------------------------------- /design/mobile-design.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OracleBrain/AgeCalc/ca05af6073ac87743334cef3b02ba5b397b24432/design/mobile-design.jpg -------------------------------------------------------------------------------- /design/test.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Year calculator app 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 | 17 |
18 | 19 |
20 |
Day
21 | 22 |

23 |
24 | 25 |
26 |
Month
27 | 28 |

29 |
30 | 31 |
32 |
Year
33 | 34 |

35 |
36 |
37 | 38 |
39 | 42 | 43 | 44 |
45 |

-- years

46 |

-- months

47 |

-- days

48 |

-- hours

49 |

-- minutes

50 | 51 |
52 |
53 |
54 | 55 | 56 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | --------------------------------------------------------------------------------