postgres-date
raw JSON → 2.1.0 verified Sat Apr 25 auth: no javascript
A focused parser that converts PostgreSQL date/time output strings into JavaScript Date objects, preserving precision and matching Postgres behavior. Version 2.1.0 supports Node >=12 and includes TypeScript types. It is maintained as a patch-level library with no minor releases planned, releasing fixes only. Unlike heavier database clients, this is a minimal single-purpose tool for accurate date parsing.
Common errors
error TypeError: (intermediate value)(...) is not a function ↓
cause Using named import `{ parse }` instead of default import.
fix
Use
import parse from 'postgres-date' or const parse = require('postgres-date'). error Cannot find module 'postgres-date' or its corresponding type declarations. ↓
cause Package not installed or TypeScript cannot resolve types.
fix
Run
npm install postgres-date and ensure tsconfig.json includes node_modules in moduleResolution. error RangeError: Invalid time value ↓
cause Parsing a date string that is out of JavaScript's Date range (e.g., year 100000).
fix
Check input dates and handle extremely large years manually.
Warnings
gotcha Infinity dates ('infinity', '-infinity') return null instead of a Date object. ↓
fix Check for null before using the result if your data may contain infinity dates.
gotcha The parser does not handle BC dates (before Christ) correctly. ↓
fix Manually handle BC dates by adjusting the year: parse('0001-01-01 BC') will not give correct year.
gotcha Parsing timestamps with time zone '00:00' will interpret as UTC, but Postgres may use different timezone offsets. ↓
fix Ensure your Postgres session timezone is set to UTC or verify the offset before parsing.
Install
npm install postgres-date yarn add postgres-date pnpm add postgres-date Imports
- default wrong
const { parse } = require('postgres-date')correctimport parse from 'postgres-date' - require (CommonJS) wrong
const parse = require('postgres-date').defaultcorrectconst parse = require('postgres-date') - parse wrong
import { parse } from 'postgres-date' parse('2011-01-23 22:15:51Z')correctimport parse from 'postgres-date' parse('2011-01-23 22:15:51Z')
Quickstart
import parse from 'postgres-date';
const dateString = '2023-12-25 10:30:00+02';
const date = parse(dateString);
console.log(date.toISOString());
// => 2023-12-25T08:30:00.000Z
// With time zone offset
const date2 = parse('2023-01-01 00:00:00-05');
console.log(date2.toISOString());
// => 2023-01-01T05:00:00.000Z
// Infinity dates
console.log(parse('infinity')); // null
console.log(parse('-infinity')); // null