Last active
March 6, 2024 09:00
-
-
Save au5ton/a029989d4dc8c08b4b08 to your computer and use it in GitHub Desktop.
Age in decimals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//You can also use this to asses the age of anything, really. | |
//For example, dynamically display how long your company has been around, when your next appointment is, etc. | |
var birthday = new Date(1998,10,22); //Change to your birthday (year,month,day) | |
//Your age in decimals, with complete precision (1000 milliseconds, 60 seconds, 60 minutes, 24 hours, 365 days) | |
var age = ((new Date() - birthday) / 1000 / 60 / 60 / 24 / 365); | |
var precision = 1000; //However many zeros is how many places (1000 returns 3 places, 1 returns 0 places) | |
//With `precision` set to 1000, you would get something like 16.495, and 1 would just be 16 | |
var result = Math.floor(age*precision)/precision; | |
//Use this to put your age somewhere in the HTML | |
document.getElementById("age").innerHTML = result; |
I made this when I was 16. I'm 25 now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Brilliant. Just what I needed!