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

@abw/badger-timestamp

v1.1.2

Published

Simple timestamp utility

Downloads

37

Readme

badger-timestamp

This is a simple module for parsing, manipulating and formatting ISO8601 timestamps, e.g. YYYY-MM-DD HH::MM::SS.

It is a very simple implementation that is intended to be lightweight alternative to more feature-rich packages like moment, luxon, dayjs, etc.

The primary goal is to be able to convert between database timestamps and human-readable strings, and to allow simple date adjustments, e.g. + 1 year, + 3 months, etc.

It is implemented as a thin wrapper around the native JS Date object.

Installation

Install badger-timestamp using your favourite package manager.

npm

npm add @abw/badger-timestamp

pnpm

pnpm add @abw/badger-timestamp

yarn

yarn add @abw/badger-timestamp

Documentation

Visit the Website for documentation and examples.

Examples

The timestamp() function provides a convenient way to create a timestamp object.

import { timestamp } from '@abw/badger-timestamp'

// create a timestamp using different string formats
const ts1 = timestamp('2023-03-07 08:45:52')
const ts2 = timestamp('2023-03-07T08:45:52')
const ts3 = timestamp('2023/03/07 08.45.52')

// using a Date object
const date = new Date('2023-03-07T08:45:52');
const ts4  = timestamp(date);

// milliseconds since Unix epoch
const ts5 = timestamp(date.getTime());
const ts6 = timestamp({ ms: date.getTime() });

// Unix seconds since epoch
const seconds = Math.round(date.getTime() / 1000);
const ts7 = timestamp({ unix: seconds });

// using component values
const ts8 = timestamp({
  year:    2023,
  month:   3,
  date:    7,
  hours:   8,
  minutes: 45,
  seconds: 52
});

When called without any arguments it returns the current date and time. Or you can use the now() function.

import { now } from '@abw/badger-timestamp'

const present = now();

Methods for getting various parts of the date and time.

const ts = timestamp('2023-03-07 08:45:52');
ts.date();          // 2023-03-07
ts.date('/');       // 2023/03/07
ts.time();          // 08:45:52
ts.time('.');       // 08.45.52
ts.year();          // 2023
ts.month();         // 3
ts.day();           // 7
ts.hours();         // 8
ts.minutes();       // 45
ts.seconds();       // 52

Methods for setting various parts of the date and time.

ts.year(2024);      // set year to 2024
ts.month(4);        // set month to 4 (April)
ts.day(8);          // set day to 8
ts.hours(9);        // set hours to 9
ts.minutes(46);     // set minutes to 46
ts.seconds(53);     // set seconds to 53

Methods for formatting parts of the timestamp.

const ts = timestamp('2023-03-07 08:45:52');
ts.kebab();         // 2023-03-07-08-45-52
ts.kebabDate();     // 2023-03-07
ts.kebabTime();     // 08-45-52
ts.kebabDateTime(); // 20230307-084552
ts.snake();         // 2023_03_07_08_45_52
ts.snakeDate();     // 2023_03_07
ts.snakeTime();     // 08_45_52
ts.snakeDateTime(); // 20230307_084552
ts.squish();        // 20230307084552
ts.squishDate();    // 20230307
ts.squishTime();    // 084552

Methods for returning other information.

ts.weekdayName();           // Tuesday
ts.weekdayName('short');    // Tue
ts.monthName();             // March
ts.monthName('short');      // Mar

Locale support (default is to use your current locale).

const ts = timestamp('2023-03-07', { locale: 'fr-FR' })
ts.weekdayName();         // mardi
ts.weekdayName('short');  // mar.
ts.monthName();           // mars
ts.monthName('short');    // mar

Boolean date/time comparison. The argument can be any of the formats accepted by the constructor, e.g. string, Date object, another timestamp object.

ts.equal('2023-03-07 08:45:52');
ts.before('2023-03-07 08:45:52');
ts.after('2023-03-07 08:45:52');

Adjusting timestamps. You can specify a string using singular or plural names (e.g. "year" or "years") with positive or negative values. Wrap-around is correctly handled (e.g. one day before "2023-02-01" is "2023-01-31").

ts.adjust("1 year 3 months");
ts.adjust({ year: 1, months: 3 });
ts.adjust({ year: -1, months: -43 });

Note that adjustments mutate the original object so you might want to call copy() first.

const ts2 = ts.copy().adjust("1 year 3 months");

Author

Andy Wardley [email protected]