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

lazyidli_date-time

v1.0.3

Published

A simple and easy-to-use TypeScript/Javascript date and time library with convenient keywords for dates, timestamps, and more.

Downloads

281

Readme

README.md

# Lazy Idli Date and Time TS

`lazyidli_time-date` is a simple, user-friendly, and lightweight TypeScript library that provides convenient utility<br/>functions for handling dates, times, UNIX timestamps, and more. It also supports easy-to-use keywords like `getToday`,<br/>`getTomorrow`, `getCurrentTime`, and includes features like age calculation, date conversions, and adding or <br/>subtracting time units.

## Features

- Get today's date, tomorrow's date, and current time with simple functions.
- Convert to and from UNIX timestamps.
- Calculate age based on date of birth.
- Add or subtract days, months, and other time units from any date.
- Get current day of the week, current month, and current week number.
- Fully written in TypeScript for type safety and modern development.

## Installation

You can install the package via npm:

```bash
npm install lazyidli_time-date

Requirements

  • Node.js: Version >=12.0
  • TypeScript: Recommended for better type safety (though the library also works with JavaScript).

Usage

Importing the Library

import {
  getToday,
  getTomorrow,
  getCurrentTime,
  getCurrentWeek,
  toUnixTimestamp,
  fromUnixTimestamp,
  calculateAge,
  addDays,
  addMonths
} from 'lazyidli_time-date';

Examples

1. Get Today’s Date

const today = getToday();
console.log(today);  // Output: '2024-08-25'

2. Get Tomorrow’s Date

const tomorrow = getTomorrow();
console.log(tomorrow);  // Output: '2024-08-26'

3. Get Current Time

const time = getCurrentTime();
console.log(time);  // Output: '14:45:30'

4. Convert to UNIX Timestamp

const unixTimestamp = toUnixTimestamp('2024-08-25');
console.log(unixTimestamp);  // Output: 1724380800

5. Convert from UNIX Timestamp

const readableDate = fromUnixTimestamp(1724380800);
console.log(readableDate);  // Output: '2024-08-25 00:00:00'

6. Calculate Age from Date of Birth

const age = calculateAge('1990-05-15');
console.log(age);  // Output: 34

7. Add Days to a Date

const newDate = addDays('2024-08-25', 10);
console.log(newDate);  // Output: '2024-09-04'

8. Add Months to a Date

const newDate = addMonths('2024-08-25', 2);
console.log(newDate);  // Output: '2024-10-25'

9. Get Current Week of the Year

const week = getCurrentWeek();
console.log(week);  // Output: 34

Full API Reference

getToday()

  • Description: Returns today's date in YYYY-MM-DD format.

  • Example:

    const today = getToday(); // '2024-08-25'

getTomorrow()

  • Description: Returns tomorrow's date in YYYY-MM-DD format.

  • Example:

    
    const tomorrow = getTomorrow(); // '2024-08-26'

getCurrentTime()

  • Description: Returns the current time in HH:mm:ss format.

  • Example:

    const time = getCurrentTime(); // '14:45:30'

getCurrentWeek()

  • Description: Returns the current week number of the year.

  • Example:

    const week = getCurrentWeek(); // 34

toUnixTimestamp(dateStr: string, format: string = 'YYYY-MM-DD')

  • Description: Converts a date string to a UNIX timestamp.

  • Parameters:

    • dateStr: The date string to convert (e.g. '2024-08-25').
    • format: Optional format of the date string. Default is 'YYYY-MM-DD'.
  • Example:

    const unixTimestamp = toUnixTimestamp('2024-08-25'); // 1724380800

fromUnixTimestamp(unixTime: number, format: string = 'YYYY-MM-DD HH:mm:ss')

  • Description: Converts a UNIX timestamp to a human-readable date string.

  • Parameters:

    • unixTime: The UNIX timestamp to convert (e.g. 1724380800).
    • format: Optional format for the output date. Default is 'YYYY-MM-DD HH:mm:ss'.
  • Example:

    const date = fromUnixTimestamp(1724380800); // '2024-08-25 00:00:00'

calculateAge(dob: string, format: string = 'YYYY-MM-DD')

  • Description: Calculates age based on the date of birth.

  • Parameters:

    • dob: The date of birth as a string (e.g. '1990-05-15').
    • format: Optional format of the date string. Default is 'YYYY-MM-DD'.
  • Example:

    const age = calculateAge('1990-05-15'); // 34

addDays(dateStr: string, days: number, format: string = 'YYYY-MM-DD')

  • Description: Adds a specified number of days to a given date.

  • Parameters:

    • dateStr: The date to which days should be added.
    • days: The number of days to add.
    • format: Optional format of the date string. Default is 'YYYY-MM-DD'.
  • Example:

    const newDate = addDays('2024-08-25', 10); // '2024-09-04'

addMonths(dateStr: string, months: number, format: string = 'YYYY-MM-DD')

  • Description: Adds a specified number of months to a given date.

  • Parameters:

    • dateStr: The date to which months should be added.
    • months: The number of months to add.
    • format: Optional format of the date string. Default is 'YYYY-MM-DD'.
  • Example:

    const newDate = addMonths('2024-08-25', 2); // '2024-10-25'

License

  • This project is licensed under the MIT License. See the LICENSE file for details.