Git Product home page Git Product logo

agecalculator's Introduction

Age Calculator

.NET GitHub GitHub issues Nuget

The Age Calculator is a library (based on the Gregorian calendar) which can be used to calculate the age between two dates and output years, months, days and time components.

In addition to the default calculation algorithm, it can be configured so that Feb 29th of a leap year be considered as Feb 28th of a non-leap year (making 1-year cycle) with switch of a flag. By default, the flag is off.

How To Use (C# Code)

/* There are three ways to calculate the age between two dates */
using AgeCalculator;
using AgeCalculator.Extensions;

public void PrintMyAge()
{
    // Date of birth or from date.
    var dob = DateTime.Parse("10/03/2015");
    
    // #1. Using the Age class constructor.
    var age = new Age(dob, DateTime.Today); // as of 09/19/2021
    Console.WriteLine($"Age: {age.Years} years, {age.Months} months, {age.Days} days, {age.Time}");
    
    // #2. Using DateTime extension.
    age = dob.CalculateAge(DateTime.Today); // as of 09/19/2021
    Console.WriteLine($"Age: {age.Years} years, {age.Months} months, {age.Days} days, {age.Time}");
    
    // #3. Using the Age type's static function.
    age = Age.Calculate(dob, DateTime.Today); // as of 09/19/2021
    Console.WriteLine($"Age: {age.Years} years, {age.Months} months, {age.Days} days, {age.Time}");
}

// Output:
// Age: 5 years, 11 months, 16 days, 00:00:00
/* In this example, Feb 29 of a leap year is considered as Feb 28 of a non leap year. */
using AgeCalculator;
using AgeCalculator.Extensions;

public void PrintAge()
{
    // Example 1
    var dob = DateTime.Parse("02/29/2016");
    var toDay = DateTime.Parse("02/28/2021");
    var age = new Age(dob, toDay, true); // <- Switch the flag ON. Default is OFF.
    Console.WriteLine($"Example 1 - Age: {age.Years} years, {age.Months} months, {age.Days} days, {age.Time}");
    
    // Example 2
    dob = DateTime.Parse("02/29/2016");
    toDay = DateTime.Parse("02/28/2021 00:00:01");
    age = dob.CalculateAge(toDay, true); // <- Switch the flag ON. Default is OFF.
    Console.WriteLine($"Example 2 - Age: {age.Years} years, {age.Months} months, {age.Days} days, {age.Time}");
    
    // Example 3
    dob = DateTime.Parse("02/29/2016 00:00:02");
    toDay = DateTime.Parse("02/28/2021 00:00:01");
    age = Age.Calculate(dob, toDay, true); // <- Switch the flag ON. Default is OFF.
    Console.WriteLine($"Example 3 - Age: {age.Years} years, {age.Months} months, {age.Days} days, {age.Time}");
}

// Output:
// Example 1 - Age: 5 years, 0 months, 0 days, 00:00:00
// Example 2 - Age: 5 years, 0 months, 0 days, 00:00:01
// Example 3 - Age: 4 years, 11 months, 27 days, 23:59:59

Default Outputs

// 02/28/2020:L - 02/29/2020:L    Age: 0 years, 0 months, 1 days, 00:00:00
// 02/29/2020:L - 02/29/2020:L    Age: 0 years, 0 months, 0 days, 00:00:00
// 02/29/2020:L - 03/01/2020:L    Age: 0 years, 0 months, 1 days, 00:00:00
// 02/29/1960:L - 02/28/2021:N    Age: 60 years, 11 months, 28 days, 00:00:00
// 02/29/1960:L - 03/01/2021:N    Age: 61 years, 0 months, 1 days, 00:00:00

// With Time Component:
// 01/05/2020 07:28:12:L - 02/05/2022 06:30:15:N    Age: 2 years, 0 months, 30 days, 23:02:03
// 02/05/2020 07:28:12:L - 01/05/2022 06:30:15:N    Age: 1 years, 10 months, 28 days, 23:02:03
// 02/05/2020 23:59:59:L - 01/06/2022 00:00:00:N    Age: 1 years, 11 months, 0 days, 00:00:01
// 02/05/2021 05:28:12:N - 02/05/2021 06:30:15:N    Age: 0 years, 0 months, 0 days, 01:02:03
// 02/05/2021 07:28:12:N - 02/06/2021 06:30:15:N    Age: 0 years, 0 months, 0 days, 23:02:03
// 02/29/2016 00:00:00:L - 02/28/2021 00:00:01:N    Age: 4 years, 11 months, 28 days, 00:00:01
// 02/29/2016 00:00:02:L - 02/28/2021 00:00:01:N    Age: 4 years, 11 months, 27 days, 23:59:59
// 02/29/2016 00:00:00:L - 03/01/2021 00:00:01:N    Age: 5 years, 0 months, 1 days, 00:00:01
// 02/29/2016 00:00:02:L - 03/01/2021 00:00:01:N    Age: 5 years, 0 months, 0 days, 23:59:59

agecalculator's People

Contributors

arman-g avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.