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

@olton/datetime

v3.2.0

Published

Datetime is a minimalist JavaScript library that parses, validates, manipulates, and displays dates and times for node and modern browsers with a comfortable modern API.

Downloads

1,207

Readme

Datetime

Coverage Status Dependencies Core size GitHub release License: MIT GitHub issues

Datetime is a minimalist JavaScript library that parses, validates, manipulates, and displays dates and times for Node and modern Browsers with a comfortable modern API.

  • 🕒 Quick and accurate
  • 💪 Immutable/mutable modes
  • 🔥 Chainable
  • 🌐 I18n support
  • 📦 9kb mini library (core), 16kb - full
  • 👫 All browsers, Electron, NodeJS supported

Getting started

Installation

In HTML

<script src="datetime.js"></script>

For NodeJS or use with Webpack or modern JS

npm install github:@olton/datetime --save

API

It's easy to use Datetime APIs to parse, validate, manipulate, and display dates and times.

Parse

datetime();
datetime("2020");
datetime("2020-12-31");
datetime("2020-12-31 23:59");
datetime(2020, 12, 31, 23, 59);
datetime([2020, 12, 31, 23, 59]);
Datetime.parse("Mon, 25 Dec 1995 13:30:00 +0430"); // Work same as Date.parse()
Datetime.from("16 November 1961 15:24", "dd mm %y h:i", "en")
Datetime.from("16 Ноября 1961 15:24", "dd mm %y h:i", "ru")

Immutable/mutable

By default, datetime object is mutable. But, You can create immutable variable with method immutable:

var immutableDate = datetime('2020-12-21').immutable();
console.log(immutableDate); // 2020-12-21
console.log(immutableDate.add(1, 'month')); // 2021-01-21 this is a new object
console.log(immutableDate); // 2020-12-21

and return to mutable state

immutableDate.immutable(false); // now immutableDate works as mutable object

Display

datetime().format('{YYYY} MM-DDTHH:mm:ss sss Z A');
datetime().strftime('{%Y} %n-%dT%H:%M:%S %Q %z %p');

Get & set

You can set and get: ms, second, minute, hour, day, month, year, time (timestamp), ...

datetime().set('month', 3).month();
datetime().month(3).month();

Manipulate

You can set: ms, second, minute, hour, day, month, year.

datetime().add(3, 'day').add(1, 'hour');
datetime().addDay(3).addHour(1);

Align (Start From)

You can align date to: second, minute, hour, day, month, year, quarter, week, isoWeek.

datetime().align("year"); // Will alignment to 1st Jan of year
datetime().align("month"); // Will alignment to 1st day of month

Compare

datetime("2020").older("2021"); // return true
datetime("2020").younger("1972"); // return true
datetime("2020").between("2019", "2021"); // return true
datetime("2020-21-12").diff("1972-21-12"); // return {day: 17532, hour: 420768, millisecond: 1514764800000, minute: 25246080, month: 576, second: 1514764800, year: 48}
datetime("2020-21-12").distance("1972-21-12", "year"); // return 48

Information

You can get different additional information about your date: count days in month, count days in year, number of quarter, year is leap, ...

datetime("2020-12-21").dayOfYear(); // return 356
datetime("2020-02-01").daysInMonth(); // return 29
datetime("2020-02-01").quarter(); // return 1
datetime("2020").isLeapYear(); // return true

i18n

Datetime has great support for internationalization. By default, Datetime includes only english locale. You can include many others:

In HTML

<script src="/DATETIME_DIR/datetime.js"></script>
<script src="/DATETIME_DIR/i18n/ru.js"></script>

For NodeJS or use with Webpack or modern JS

import "@olton/datetime";
import "@olton/datetime/i18n/ru";

With locales:

Datetime.fromString("16 Ноября 1961 15:24", "dd mm %y h:i", "ru");
datetime().useLocale('ru').format("DD MMM YYYY"); // 03 Дек 2020

Plugins

You can create plugin and register it with functions Datetime.use() and Datetime.useStatic():

Create plugin

(function(global) {
    'use strict';

    Datetime.use({
        prototypeTest: function(val){
            return 0 === val || val ? val : "test";
        }
    });

    Datetime.useStatic({
        staticTest: function(val){
            return 0 === val || val ? val : "static test";
        }
    });
}());

Include a plugin into page after datetime.js:

<script src="datetime.js"></script>
<script src="plugin.js"></script>

And now use plugin:

<!-- Prototype methods -->
console.log(datetime().prototypeTest());
console.log(datetime().prototypeTest(123));

<!-- static methods  -->
console.log(Datetime.staticTest());
console.log(Datetime.staticTest(456));

Sponsors

No sponsor yet :(

License

Datetime is licensed under a MIT license.