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

carbona

v1.0.6

Published

A simple JavaScript date and time library inspired by PHP's Carbon

Downloads

20

Readme

Carbona

Carbona is a simple JavaScript date and time library inspired by PHP's Carbon library. It provides an easy-to-use API for manipulating and formatting dates and times.

Installation

You can install Carbona via npm:

npm install Carbona

Usage

Importing the Library

In your JavaScript file, import the Carbona class:

const Carbona = require("Carbona");

Creating a Carbona Instance

You can create a new instance of Carbona with the current date and time or a specific date and time:

// Current date and time
let now = Carbona.now();

// Specific date and time
let specificDate = Carbona.parse("2024-08-08 15:00:00");

Formatting Dates

Use the format method to format the date. The format string supports the following tokens:

  • YYYY - Full year (e.g., 2024)
  • YY - Short year (e.g., 24)
  • MMMM - Full month name (e.g., August)
  • MMM - Short month name (e.g., Aug)
  • MM - Padded month (e.g., 08)
  • M - Non-padded month (e.g., 8)
  • DD - Padded day (e.g., 08)
  • D - Non-padded day (e.g., 8)
  • Do - Day with suffix (e.g., 8th)
  • HH - Hours (e.g., 14)
  • mm - Minutes (e.g., 05)
  • ss - Seconds (e.g., 09)

Example:

let formattedDate = now.format("Do MMMM YYYY, HH:mm:ss");
console.log(formattedDate); // Outputs something like '8th August 2024, 15:00:00'

Manipulating Dates

Carbona allows you to add or subtract days, months, or years from a date:

// Add days
let futureDate = now.addDays(5).format("YYYY-MM-DD");
console.log(futureDate); // Outputs the date 5 days from now

// Subtract months
let pastDate = now.subMonths(2).format("YYYY-MM-DD");
console.log(pastDate); // Outputs the date 2 months ago

Other Useful Methods

  • Start and End of Day: Set the time to the start or end of the day.

    let startOfDay = now.startOfDay().format("YYYY-MM-DD HH:mm:ss");
    let endOfDay = now.endOfDay().format("YYYY-MM-DD HH:mm:ss");
  • Difference in Days: Calculate the difference in days between two dates.

    let diff = now.diffInDays("2024-08-15");
    console.log(diff); // Outputs the difference in days
  • Comparison: Check if a date is before or after another date.

    let isBefore = now.isBefore("2024-08-10");
    let isAfter = now.isAfter("2024-08-05");

Convert to Native Date Object

You can convert the Carbona instance to a native JavaScript Date object:

let nativeDate = now.toDate();
console.log(nativeDate); // Outputs the native Date object

Convert to ISO String

Convert the Carbona instance to an ISO string:

let isoString = now.toISOString();
console.log(isoString); // Outputs the ISO string

Convert to String

Convert the Carbona instance to a string representation:

let dateString = now.toString();
console.log(dateString); // Outputs the string representation of the date

License

This project is licensed under the MIT License.