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

strtime

v1.1.2

Published

Comprehensive strftime and strptime implementation.

Downloads

3,632

Readme

strtime

MIT License Build Status NPM version

The strtime package provides native JavaScript implementations of the strftime and strptime functions. It supports most of the combined features of Python, Ruby, and GNU C strftime and strptime functions. It's possible to write and parse calendar dates (e.g. %Y-%m-%d), week dates (e.g. %G-W%V-%u), and ordinal dates (e.g. %Y-%j).

Installation

You can add the strtime package to your project using a JavaScript package manager such as npm.

npm install --save strtime

Directives

You can read complete documentation regarding the directives which strtime supports (e.g. %Y, %b) and how they behave in directives.md.

Basic usage

The strftime function accepts Date objects, unix timestamps (as milliseconds since UTC epoch), moment datetime objects, luxon datetime objects, and dayjs datetime objects. The strptime function always outputs a Date object.

const strftime = require("strtime").strftime;
const strptime = require("strtime").strptime;

// Prints "2000-01-01T00:00:00.000Z"
console.log(strftime(new Date("2000-01-01"), "%Y-%m-%dT%H:%M:%S.%LZ"));

// Prints "2000-01-01T00:00:00.000Z"
const date = strptime("2000-01-01T00:00:00.000Z", "%Y-%m-%dT%H:%M:%S.%LZ");
console.log(date.toISOString());

Advanced usage

Timezone output with strftime

The strftime function defaults to writing a UTC timestamp. You can specify which timezone should be used by passing it as an argument. Timezones are accepted as numeric offsets or as abbreviations such as UTC or EDT or EEST. Offsets between and including -16 and +16 are interpreted as hour offsets. Other offset values are interpreted as minute offsets. You can also use the string local to use the local timezone.

// Prints "2000-01-01 12:00:00 GMT+0000"
console.log(strftime(new Date("2000-01-01T12:00:00Z"), "%Y-%m-%d %H:%M:%S GMT%z"));

// Prints "2000-01-01 14:00:00 GMT+0200"
console.log(strftime(new Date("2000-01-01T12:00:00Z"), "%Y-%m-%d %H:%M:%S GMT%z", +2));

// Prints "2000-01-01 08:00:00 GMT-0400"
console.log(strftime(new Date("2000-01-01T12:00:00Z"), "%Y-%m-%d %H:%M:%S GMT%z", "EDT"));

Timezone assumption with strptime

The strptime function assumes that a timestamp represents a UTC date if no timezone is specified in that timestamp. You can specify what timezone should be assumed for timestamps which do not contain an explicit timezone by passing it as an argument. The strptime function accepts timezones arguments in the same way that strftime does.

// Prints e.g. "2000-01-01T12:00:00.000Z"
const date = strptime("2000-01-01 12:00:00", "%Y-%m-%d %H:%M:%S");
console.log(date.toISOString());

// Prints "2000-01-01T10:00:00.000Z" due to the +2 hours offset
const date = strptime("2000-01-01 12:00:00", "%Y-%m-%d %H:%M:%S", +2);
console.log(date.toISOString());

Locale-dependent strings

The strftime and strptime functions default to English weekday names, month names, and ordinals. However, it is possible to specify different text by passing an options object.

The options object attributes which are recognized for this purpose are:

  • shortWeekdayNames: A list of abbreviated weekday names, e.g. "Sun", "Mon".
  • longWeekdayNames: A list of full weekday names, e.g. "Sunday", "Monday".
  • shortMonthNames: A list of abbreviated month names, e.g. "Jan", "Feb".
  • longMonthNames: A list of full month names, e.g. "January", "February".
  • eraNames: A list of era names, e.g. "CE", "BCE".
  • meridiemNames: A list of meridiem strings, e.g. "AM", "PM".
  • ordinalTransform: A function accepting a number and outputting an ordinal suffix, e.g. 1 => "st".
const monthNames = [
    "enero", "febrero", "marzo", "abril", "mayo", "junio",
    "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"
];

// Prints "1 enero 2000"
console.log(strftime(new Date("2000-01-01"), "%-d %B %Y", +0, {
    longMonthNames: monthNames
}));