Skip to content

Instantly share code, notes, and snippets.

@au5ton
Last active March 6, 2024 09:00
Show Gist options
  • Save au5ton/a029989d4dc8c08b4b08 to your computer and use it in GitHub Desktop.
Save au5ton/a029989d4dc8c08b4b08 to your computer and use it in GitHub Desktop.
Age in decimals
//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;
@jos3ph1205
Copy link

Brilliant. Just what I needed!

@au5ton
Copy link
Author

au5ton commented Mar 6, 2024

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