npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

age-to-birth-date

v1.1.4

Published

Can calculate birth date for a given age, and other such things

Downloads

10

Readme

Age to Birth Date calculator

This package is a simple tool to calculate the earliest or latest possible date of birth for a given age. Of course with typescript support.

npm install age-to-birth-date
import {
    latestBirthDateForAge, 
    earlistBirthDateForAge,
} from 'age-to-birth-date'

// Assuming todays date is 2019-09-10 (00:00z)

earlistBirthDateForAge(20); // Date(1998-09-10)
latestBirthDateForAge(20); // Date(1999-09-10)

// CommonJS
const ageToBirth = require('age-to-birth-date');
console.log(ageToBirth.earlistBirthDateForAge(20)); // Date(1998-09-10)

The example above is inclusive, meaning that if you were born exactly on 1999-09-10, you would still be regarded as being 20 years of age (actually you would be 20 years and 0 days).

Likewise, if you were born at 1998-09-10 you would be regarded as 20, although the actual age is 21 years and 0 days.

This can be prevented by passing false as the second parameter:

// Assuming todays date is 2019-09-10 (00:00z)

earlistBirthDateForAge(20, false); // Date(1998-09-11) (19 years, 11 months, 30 days) 
latestBirthDateForAge(20, false); // Date(1999-09-11) (18 years, 11 months, 30 days)

Basic comparisons

To make sure that some one is at least 10 years old.

// Assuming todays date is 2019-09-10 (00:00z)
const usersBirthDate = new Date('2008-05-10:00:00:00z');

const latestFor10YO = latestBirthDateForAge(10) // 2009-09-10
const foo = usersBirthDate < latestFor10YO; // true

Dates between an age span

Package also includes dateRangeForAgeSpan that returns a object with from and to dates calculated from the input ages. For example:

import { dateRangeForAgeSpan } from 'age-to-birth-date'

// Assuming todays date is 2019-09-10 (00:00z)

const foo = dateRangeForAgeSpan(10, 20);
/**
    foo = {
        from: Date(1998-09-10:00:00:00z)
        to: Date(2009-09-10:00:00:00z)
    }
*/

This method also accepts an optional third boolean parameter to not be include this day:

// Assuming todays date is 2019-09-10 (00:00z)

const foo = dateRangeForAgeSpan(10, 20, false);
/**
    foo = {
        from: Date(1998-09-11:00:00:00z)
        to: Date(2009-09-11:00:00:00z)
    }
*/

Why did I do this?

Working with dates and ages always gets to me - it seems I just can't wrap my head around if I am supposed to subtract or add years and days - and in what direction to get what results. And so on... Every time I end up spending a lot of time just trying to understand. Perhaps it's just me that are slow in this sense...

But if there is some one else out there that hates calculating ages and birth dates - feel free to use this package.