1.0.2 • Published 3 years ago
age2 v1.0.2
Quality Package Template
Light and fully documented Age object for TS and JS
📦 Installation
- Using
npmnpm i age2 - Using
Yarnyarn add age2 - Using
pnpmpnpm add age2
⚙️ Usage
How to import
- ES Modules
import { Age } from "age2"; - Common JS
const { Age } = require("age2");
How to construct
Age constructor is exactly the same as Date's
Cause age is determined by underlying date.
Ways to construct
- Date string
const age = new Age("1970-01-01 11:25:04"); - Milliseconds since 1st of jan. 1970
const age = new Age(19504000); - By passing a date object
const age = new Age(new Date("1970-01-01 11:25:04")); - By passing each date part separately
const age = new Age( 1970, // year 0, // month (january) 0, // Optional: day of the mons 0, // Optional: hours 0, // Optional: minutes 0, // Optional: seconds 0 // Optional: milliseconds );
Properties
valueconsole.log(age.value); // 52dateOfBirthconsole.log(age.dateOfBirth.toISOString()); // "1970-01-01T08:25:04.000Z"dayOfBirthconsole.log(age.dateOfBirth.toISOString()); // "1970-01-01T00:00:00.000Z"isBirthday(date?)console.log(age.isBirthday(/* default: now */)); // false console.log(age.isBirthday("2021-01-01")); // true console.log(age.isBirthday("2021-01-02")); // false
Serialization
Date object serializes as a number
console.log(age.value); // 52
console.log(age.valueOf); // 52
console.log(+age); // 52
console.log(String(age)); // 52
console.log(JSON.stringify(age)); // 52