Skip to content

Commit e1cf1c4

Browse files
committed
add dynamic age calculation
1 parent 13bd6c3 commit e1cf1c4

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/routes/+page.svelte

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,28 @@
88
import NextLogo from "$lib/assets/logos/Nextjs-logo.svg?component";
99
import ReactLogo from "$lib/assets/logos/React-icon.svg?component";
1010
import VueLogo from "$lib/assets/logos/Vue.js_Logo_2.svg?component";
11+
12+
const getAge = (dateString: string) => {
13+
var today = new Date();
14+
var birthDate = new Date(dateString);
15+
var age = today.getFullYear() - birthDate.getFullYear();
16+
var m = today.getMonth() - birthDate.getMonth();
17+
18+
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
19+
age--;
20+
}
21+
22+
return age;
23+
}
24+
1125
</script>
1226

1327
<main>
1428
<h1>About me</h1>
1529
<article>
1630
<p>Hello! My name is <b>Károly Sárdi</b>, also known as <b>skver</b>.</p>
1731
<p>
18-
I'm a 20 years old student from Hungary, currently studying at the <br /> Eötvös
32+
I'm a {getAge("2003-02-25")} years old student from Hungary, currently studying at the <br /> Eötvös
1933
Loránd University, Faculty of Informatics.
2034
</p>
2135
<p>

0 commit comments

Comments
 (0)