Giter Site home page Giter Site logo

age-calculator's Introduction

Introduction

Javascript library to calculate age from date of birth

Installation

npm install @dipaktelangre/age-calculator

Usage

node

var { AgeCalculator } = require("@dipaktelangre/age-calculator"); // undefined
AgeCalculator.getAge(new Date("01/01/1990")); //{ years: 30, months: 6, days: 3 }
AgeCalculator.getAgeIn(new Date("01/01/1990"), "years"); // 30
AgeCalculator.getAgeIn(new Date("01/01/1990"), "weeks"); // 1591
AgeCalculator.getAgeIn(new Date("01/01/1990"), "month"); //366

typescript

import { AgeCalculator } from "@dipaktelangre/age-calculator";
AgeCalculator.getAge(new Date("01/01/1990")); //{ years: 30, months: 6, days: 3 }
AgeCalculator.getAgeIn(new Date("01/01/1990"), "years"); // 30
AgeCalculator.getAgeIn(new Date("01/01/1990"), "weeks"); // 1591
AgeCalculator.getAgeIn(new Date("01/01/1990"), "month"); //366

Build

npm run build

Test

npm run test
npm run test:tdd

Contribute

If you would like to contribute, you are welcome. Clone repository and open pull request.

age-calculator's People

Contributors

dipaktelangre avatar dependabot[bot] avatar

Stargazers

Omar Ibrahim avatar Ou Fumio avatar  avatar  avatar

Watchers

James Cloos avatar  avatar

age-calculator's Issues

I wish this library was pure function.

This library is very useful.

Sometimes I want to display a person's age or other information on a date other than today.

example

 /**
   * Get age in years, month, days format from given date of birth
   * @param dob date of birth
   * @param targetDate targetDate
   * @returns age in {years, months, days}
   */
  static getAgePure(dob: Date, targetDate: Date): Age {
    if (!(dob instanceof Date)) throw new Error("dob is Invalid date");
    if (!(targetDate instanceof Date)) throw new Error("targetDate is Invalid date");
    if (moment(dob.toISOString()).isAfter(moment())) throw new Error("Future date not allowed");
    const dateCal = new DateCalculator();
    return dateCal.dateDifference(dob, targetDate);
  }

/**
   * Get age in given formate
   * @param dob date of birth
   * @param targetDate targetDate
   * @param format unit of age i.g. years, months, weeks, days, hours etc
   */

  static getAgeInPure(dob: Date, targetDate: Date, format: UnitOfAge): number {
    if (!(dob instanceof Date)) throw new Error("Invalid date");
    if (!(targetDate instanceof Date)) throw new Error("targetDate is Invalid date");
    if (moment(dob.toISOString()).isAfter(moment())) throw new Error("Future date not allowed");
    const dateCal = new DateCalculator();
    return dateCal.dateDifferenceIn(dob, targetDate, format);
  }

How about something like this.

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.