Math function to calculate age.
How to Create an Age Calculator Function in Function Calculator
One practical function you can create in Function Calculator is an age calculator.
With just a few lines of code, you can calculate a person’s age from their birth year.
Step 1: Understand the now() function
In Function Calculator, the built-in function now() returns the current date and time as a 14-digit integer in the format:
YYYYMMDDHHMMSS
For example:
20250915162430
represents 2025-09-15 16:24:30 (4:24:30 pm).
Step 2: Extract the current year
Since the first four digits represent the year, we can isolate them by dividing the number by 10^10 and applying floor().
def year() {
var date = now();
return floor(date / 10^10);
}
This function takes the 14-digit number from now(), removes the last 10 digits (month, day, time), and leaves only the year.
For example:
floor(20250915162430 / 10^10) = 2025
Step 3: Create the age function
Now, we can calculate age by subtracting the birth year from the current year:
def age(birthYear) {
var thisYear = year();
return thisYear - birthYear;
}
---
Step 4: Use the function
For example:
age(1990)
will return:
35
if the current year is 2025.
After coding don't forget to press run button to implement your code and press save button for later use the function.
This is a simple but powerful way to build practical everyday functions with Function Calculator.
Comments
Post a Comment