Registry / database / pg-interval

pg-interval

JSON →
library1.0.8jsnpmunverified

A lightweight JavaScript library for parsing, formatting, and performing arithmetic on PostgreSQL interval strings. Current version 1.0.8 provides tools to create interval objects from 'HH:MM:SS' strings, format them with custom templates, and perform add, subtract, multiply, and diff operations. Unlike heavier libraries like moment.js or Luxon, pg-interval focuses solely on the PostgreSQL interval format, making it ideal for simple interval manipulations without dependencies. The library mutates objects for add/subtract but offers a clone option for multiply. It is primarily CommonJS with no ESM exports.

npm install pg-interval
INSTALL
IMPORT
SIG · PG-INTERVAL
P
pg-interval
databasejavascriptv1.0.8
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const pgi = require('pg-interval');
import pgi from 'pg-interval';
Package does not provide ESM exports; only CommonJS require works.
default
const pgi = require('pg-interval');
const { pgi } = require('pg-interval');
The default export is the function itself, not an object named pgi.
default
const pgi = require('pg-interval');
const pgi = require('pg-interval').default;
No .default property exists; direct require returns the function.

Shows creation, formatting, addition, diff, and multiply (with clone) using pg-interval.

const pgi = require('pg-interval'); const interval = pgi('01:30:00'); console.log(interval.format('hh hours mm minutes ss seconds')); // "01 hours 30 minutes 00 seconds" const addResult = pgi('00:15:00').add(interval); console.log(addResult.format()); // "01:45:00" const diff = pgi('02:00:00').diff(pgi('01:00:00')); console.log(diff.format('hh:mm')); // "01:00" const multiplied = pgi('00:10:00').multiply(3, true); console.log(multiplied.format()); // "00:30:00"
Debug
Known issues
gotchaadd() and subtract() mutate the original pgi object; they do not return a new instance.
fix
Clone the object manually or use multiply() with clone=true if you need immutability.
affects: <=1.0.8
gotchamultiply() without the second argument mutates the original object.
fix
Use multiply(factor, true) to get a new instance without modifying the original.
affects: <=1.0.8
gotchaThe library only accepts input in 'HH:MM:SS' format; other PostgreSQL interval strings (like '1 day' or '1 month') will not parse.
fix
Convert other interval formats to 'HH:MM:SS' before passing to pgi().
affects: <=1.0.8
gotchaNo support for negative intervals or overflow (e.g., hours > 23 may produce unexpected output).
fix
Avoid using intervals with hours > 23 or negative values; limit to timestamp-difference style intervals.
affects: <=1.0.8
Errors
Common errors & fixes
TypeError: pgi is not a function
Using ES import syntax on a CommonJS-only package.
fix
Change to `const pgi = require('pg-interval');`
ReferenceError: require is not defined
Trying to use require in an ESM context (e.g., type: 'module' in package.json).
fix
Use dynamic import: `const pgi = await import('pg-interval');` or convert to CommonJS.
Uncaught TypeError: pgi(...).add is not a function
Attempting to call add() on a string instead of a pgi object.
fix
Ensure both operands are created via pgi(): `pgi('00:01:00').add(pgi('00:02:00'))`
Upgrade
Version history
1.0.8latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
pg-interval — npm install pg-interval · libregistry