humanize-age
Convert differences of dates into human-readable strings.
What is humanize-age?
humanize-age is a very simple npm package which makes human-readable strings like "2 years and 3 months old" from the birthday and a specifed date.
How to Use
const humanizeAge = ; const myBirthday = '1984-09-24'; /* How old were I on the first new year's day? */const newYearsDay = '1985-01-01';console; /* How old were I when Atlanta Olympic started? */const atlantaOlympic = '1996-07-19';console; /* And how old am I? */const today = ;console;
API
getHumanizedAge(birthday, date)
Returns string which represents the age in human-readable form. For example, first method call in the example code above returns "3 months and 8 days old".
If s/he is younger than 1 month old, this method returns age in days (e.g.: "Day 14").
If s/he is younger than 1 year old, this method returns age in months (e.g.: "3 months old").
If s/he is younger than 15 year old, this method returns age in years and months (e.g.: "8 years and 3 months old").
If s/he is older than or equal to 15 year old, this method returns age in years (e.g.: "30 years old").
difference(d1, d2)
Returns object which represents difference of specified two dates. d1
must be smaller than or equal to d2
.
For example, humanizeAge.difference(new Date("1984-09-24"), new Date("2017-01-09"))
returns { y: 32, m: 3, d: 16 }
Note
Ages will be calculated assuming that specified dates are 00:00 in UTC.
Author information
Programmed by kcrt (TAKAHASHI, Kyohei) http://profile.kcrt.net/
License
Copyright © 2016 kcrt (TAKAHASHI, Kyohei)
Released under the MIT license
http://opensource.org/licenses/mit-license.php
Reference
none