[Free] Age calculator Extension

Hello mit app invetor developers ,
this is my age calculator Extension created by Mr_koder
icons8-age-468

Documentation

  1. Calculate Age Block

blocks (4)

  • Description: This block calculates the age of a person in years based on the given birthday.
  • Input: A string representing the person's birthday in the format "yyyy-MM-dd"
  • Output: An integer representing the person's age in years.

  1. Calculate Age in Days Block

blocks (5)

  • Description: This block calculates the age of a person in days based on the given birthday.
  • Input: A string representing the person's birthday in the format "yyyy-MM-dd"
  • Output: An integer representing the person's age in days.

  1. Calculate Age in Months Block

blocks (6)

  • Description: This block calculates the age of a person in months based on the given birthday.
  • Input: A string representing the person's birthday in the format "yyyy-MM-dd"
  • Output: An integer representing the person's age in months.

  1. Is Birthday Today Block

blocks (7)

  • Description: This block checks if today is the person's birthday based on the given birthday.
  • Input: A string representing the person's birthday in the format "yyyy-MM-dd"
  • Output: A boolean value, true if today is the person's birthday, and false otherwise.

Download

com.age.calculate.mrkoder.aix (6.2 KB)

Thanks .

Mr_koder

Again, this is easily achievable by blocks. Please focus on something that is not achievable by blocks.

1 Like

Good document.
but you did not follow the naming convention. Function name should be like UpperCase.

And all these functions can be done easily by native blocks, why bother to use a extension?

@Gordon_Lu
@Kevinkun
I already edited the naming of block in the aix file
is there are any other problems
you can check it in the file

I used it to reduce the number of blocks in the procedure of making this calculation

then modify block image of the document in first post.

Unless if it is complicated (like Saj's List extension) or for organization purposes (packaging a lot of functions into an extension), there's no such need for test extensions. Like your previous extensions, please focus on something not achievable by blocks.

1 Like

ok, I understand I will take your advice in my mind,
I am just a beginner at creating extensions.
thank @Gordon_Lu

over all this is source code :

package com.age.be.dev;

import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.util.Calendar;

@DesignerComponent(
        version = 1,
        description = "Calculate age based on a given date is created by Mr_koder  https://www.youtube.com/channel/UCMjrKCO3Y8_i1I_WOw6QQzw",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "https://i.ibb.co/RCbDMbp/Icon.png")

@SimpleObject(external = true)
// Libraries
@UsesLibraries(libraries = "")
// Permissions
@UsesPermissions(permissionNames = "")

public class Age extends AndroidNonvisibleComponent {

    // Activity and Context
    private Context context;
    private Activity activity;

    public Age(ComponentContainer container){
        super(container.$form());
        this.activity = container.$context();
        this.context = container.$context();
    }



    

    @SimpleFunction(description = "Calculate age based on the provided birthday in the format yyyy-MM-dd")
    public static int CalculateAge(String birthday) {
        Calendar today = Calendar.getInstance();
        Calendar birthdate = Calendar.getInstance();

        birthdate.set(Integer.parseInt(birthday.substring(0, 4)), Integer.parseInt(birthday.substring(5, 7)) - 1, Integer.parseInt(birthday.substring(8, 10)));

        int age = today.get(Calendar.YEAR) - birthdate.get(Calendar.YEAR);
        if (today.get(Calendar.MONTH) < birthdate.get(Calendar.MONTH) || (today.get(Calendar.MONTH) == birthdate.get(Calendar.MONTH) && today.get(Calendar.DAY_OF_MONTH) < birthdate.get(Calendar.DAY_OF_MONTH))) {
            age--;
        }

        return age;
    }






    @SimpleFunction(description = "Calculate age in days based on the provided birthday in the format yyyy-MM-dd")
public static int CalculateAgeInDays(String birthday) {
    Calendar today = Calendar.getInstance();
    Calendar birthdate = Calendar.getInstance();

    birthdate.set(Integer.parseInt(birthday.substring(0, 4)), Integer.parseInt(birthday.substring(5, 7)) - 1, Integer.parseInt(birthday.substring(8, 10)));

    long diffInMillis = today.getTimeInMillis() - birthdate.getTimeInMillis();
    return (int) (diffInMillis / (1000 * 60 * 60 * 24));
}







@SimpleFunction(description = "Calculate age in months based on the provided birthday in the format yyyy-MM-dd")
public static int CalculateAgeInMonths(String birthday) {
    Calendar today = Calendar.getInstance();
    Calendar birthdate = Calendar.getInstance();

    birthdate.set(Integer.parseInt(birthday.substring(0, 4)), Integer.parseInt(birthday.substring(5, 7)) - 1, Integer.parseInt(birthday.substring(8, 10)));

    int years = today.get(Calendar.YEAR) - birthdate.get(Calendar.YEAR);
    int months = today.get(Calendar.MONTH) - birthdate.get(Calendar.MONTH);

    return years * 12 + months;
}






@SimpleFunction(description = "Check if today is the birthday based on the provided birthday in the format yyyy-MM-dd")
public static boolean IsBirthdayToday(String birthday) {
    Calendar today = Calendar.getInstance();
    Calendar birthdate = Calendar.getInstance();

    birthdate.set(Integer.parseInt(birthday.substring(0, 4)), Integer.parseInt(birthday.substring(5, 7)) - 1, Integer.parseInt(birthday.substring(8, 10)));

    return (today.get(Calendar.MONTH) == birthdate.get(Calendar.MONTH)) && (today.get(Calendar.DAY_OF_MONTH) == birthdate.get(Calendar.DAY_OF_MONTH));
}


}

edited

You forgot to follow the naming conventions, this also includes the screenshots

Also please provide App Inventor blocks screenshots

Taifun

1 Like