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

t4mat

v0.0.6

Published

Tiny & easy to use library for date formatting

Downloads

8

Readme

t4mat

Tiny (1 KB when miniified and gzipped) & easy to use library for date formatting

Installation

Download

Download the t4mat.min.js file from the dist directory.

NPM

npm install --save t4mat

Usage

Note: This library can be either used in node or in the browser. The following examples are using the node environment.

const t4mat = require("t4mat");
var myDate = "2016-01-01T16:40:01.083Z"; // ISO string
var formatted = t4mat({
	t:myDate,
	format:"{yyyy} - {m} - {d}"
});

// > "2016 - 1 - 1"

As the above example demonstrates, the library is a mere function that takes an object as an argument, this object would have the following properties:

  • time (aliases: t1,t): The date in question, which can be a milliseconds since the epoch time (e.g. 1451666401083), an ISO String like the example above, or a date object. In fact it can be any value that the new Date() constructor in javascript can take.
  • format: This is just a string having short codes that are delimited by the curly braces. So for the above example, this format: {d} / {m} would return: 1 / 1, while this format: {yyyy} ({d}) would return: 2016 (1). The following shortcodes are available in the format:
    • {d} The day of the month (Example: 1).
    • {dd} The day of the month with a 0 prefix when it's less than 10 (Example: 01).
    • {D} The day of the week, short version (Example: Wed.).
    • {DD} The day of the week, full version (Example: Wednesday).
    • {m} The numerical representation of the month (Example: 1).
    • {mm} The numerical representation of the month, with a 0 prefix when it's less than 10 (Example: 01).
    • {M} The literal representation of the month, short version (Example: Jan.).
    • {MM} The literal representation of the month, full version (Example: January).
    • {Y} Two digits representation of the year (Example: 16).
    • {y} same as above.
    • {YY} same as above.
    • {yy} same as above.
    • {YYYY} Full year (Example 2016).
    • {yyyy} same as above.
    • {yyy} same as above.
    • {YYY} same as above.
    • {R} Relative representation of the date/time, short version (Example 4 yrs ago).
    • {RR} Relative representation of the date/time, full version (Example 4 years ago).

About the relative time

By default the time you pass in the object will be represented relatively to the current time. However, if you want it to be calculated relative to another time, pass the base time as base in the object.

  • Example:

Let's suppose that the current time in the browser is 2016-01-02T00:00:00.000Z

Running this code:

var formatted = t4mat({
	time:"2016-01-01T00:00:00.000Z",
	format:"{RR}"
});
console.log(formatted);
// > "1 day ago"

var formatted2 = t4mat({
	time:"2016-01-03T00:00:00.000Z",
	format:"{RR}"
});
console.log(formatted2);
// > "in 1 day"

So by default the relative time calculation takes the current browser/machine time as the base. However if you wanted to pass your own base time then pass it as t2:

var formatted = t4mat({
	time:"2016-01-01T00:00:00.000Z",
	base:"2016-01-02T00:00:00.000Z"
	format:"{RR}"
});
console.log(formatted);
// > "1 day ago"

var formatted2 = t4mat({
	time:"2016-01-01T00:00:00.000Z", // same as above
	base:"2016-01-03T00:00:00.000Z" // different base time
	format:"{RR}"
});
console.log(formatted2);
// > "2 day ago"