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

moment-db

v2.0.0

Published

Moment implementation for formatting Dates into ANSI compatible strings suitable for database consumption

Downloads

5

Readme

moment-db

Moment implementation for formatting Dates into ANSI compatible strings suitable for database consumption.

npm install moment-db

import MomentDB from 'moment-db.js'

Assume the date/time notation below.

| Notation | Description | :--- | :--- | YYYY | 4-digit year | MM | 2-digit month (01 to 12) | DD | 2-digit day (01 to 31) | HH | 2-digit hour (00 to 23) | MI | 2-digit minute (00 to 59) | SS | 2-digit second (00 to 59) | FFF | Fraction of a second (1 to 9 digits) | TH | 2-digit hour offset (-12 to 14) | TM | 2-digit minute offset (00 to 59) | YEARS | Number of years (max of 9999) | DAYS | Number of days (max of 3652047)

Using the forementioned notation, the following database data types can be used to construct ANSI compliant date/time formats:

| Data Type | Format | Example | :--- | :--- | :--- | DATE | YYYY-MM-DD | 2030-01-31 | TIME (w/o time zone) | HH:MI:SS.FFF | 12:01:20.903 | TIME (with time zone) | HH:MI:SS.FFF [+|-]TH:TM | 12:01:20.903 -07:00 | TIMESTAMP (w/o time zone) | YYYY-MM-DD HH:MI:SS.FFF | 2030-01-31 12:01:20.903 | TIMESTAMP (with time zone) | YYYY-MM-DD HH:MI:SS.FFF [+|-]TH:TM | 2030-01-31 12:01:20.903 -07:00 | INTERVAL YEAR TO MONTH | [+|-]YEARS-MM | +130-01 | INTERVAL DAY TO SECONDS | [+|-]DAYS HH:MI:SS.FFF | -47482 12:01:20.903

Converting ES Dates

Each static MomentDB function accepts a Date that can be formatted into an ANSI compliant date/time format suitible for database consumption.

DATE

const date = MomentDB.date(new Date());
// assumming date, the output would be something like:
// 2030-01-31

TIME (w/o time zone)

const time = MomentDB.time(new Date(), true);
// assumming date, the output would be something like:
// 12:01:20.903

TIME (with time zone)

const time = MomentDB.time(new Date());
// assumming date/timezone, the output would be something like:
// 12:01:20.903 -07:00

TIMESTAMP (w/o time zone)

const ts = MomentDB.timestamp(new Date(), true);
// assumming date, the output would be something like:
// 2030-01-31 12:01:20.903

TIMESTAMP (with time zone)

const ts = MomentDB.timestamp(new Date());
// assumming date/timezone, the output would be something like:
// 2030-01-31 12:01:20.903 -07:00

INTERVAL (year to month)

const date1 = new Date(Date.UTC(1900, 00, 01)), date2 = new Date();
const monthDate = new Date();
monthDate.setMonth(0);
const interval = MomentDB.intervalYearToMonth(date1, date2, monthDate);
// output (assuming we are in year 2030):
// +130-01

INTERVAL (day to second)

const date1 = new Date(), date2 = new Date(Date.UTC(1900, 00, 01));
const timeDate = new Date();
const interval = MomentDB.intervalDayToSecond(date1, date2, timeDate);
// output (assuming we are in year 2030 and the time is the as indicated in timeDate):
// -47482 12:01:20.903

Converting formatted dates/times into ES Dates

Each static MomentDB function accepts a string returned from prior call to one of the string conversion functions that will be converted back into a Date.

DATE

const date = MomentDB.date('2030-01-31');
const iso = date.toISOString();
// outputs a Date with the date set as UTC
// 2030-01-31T00:00:00.000Z

TIME (w/o time zone)

const time = MomentDB.time('12:01:20.903');
const iso = time.toISOString();
// outputs a Date with the time set as UTC
// iso output (date defaults to 0000-01-01):
// 0000-01-01T12:01:20.903Z

// can also remove the timezone, if needed:
const time2 = MomentDB.time('12:01:20.903 -07:00', true);
const iso2 = time2.toISOString();
// outputs a Date with the time set as UTC (no timezone)
// iso2 output (date defaults to 0000-01-01):
// 0000-01-01T12:01:20.903Z

TIME (with time zone)

const time = MomentDB.time('12:01:20.903 -07:00');
const iso = time.toISOString();
// outputs a Date with the time set with the indicated zone
// iso output (date defaults to 0000-01-01):
// 0000-01-01T19:29:38.903Z

TIMESTAMP (w/o time zone)

const ts = MomentDB.timestamp('2030-01-31 12:01:20.903');
const iso = ts.toISOString();
// outputs a Date with the date/time set as UTC
// iso output:
// 2030-01-31T12:01:20.903Z

// can also remove the timezone, if needed:
const ts2 = MomentDB.timestamp('2030-01-31 12:01:20.903 -07:00', true);
const iso2 = ts2.toISOString();
// outputs a Date with the date/time set as UTC (no timezone)
// iso2 output:
// 2030-01-31T12:01:20.903Z

TIMESTAMP (with time zone)

const ts = MomentDB.timestamp('2030-01-31 12:01:20.903 -07:00');
const iso = ts.toISOString();
// iso output:
// 2030-01-31T19:01:20.903Z

INTERVAL (year to month)

const date = MomentDB.intervalYearToMonth('+130-02');
const iso = date.toISOString();
// iso output (assuming the current year is 2030):
// 2160-02-01T00:00:00.000Z

// can also pass a reference date for the year instead of the default current date
const date2 = MomentDB.intervalYearToMonth('+130-02', new Date(Date.UTC(2030)));
const iso = date.toISOString();
// iso output (assuming today is 2030-02-03 and time in UTC is as indicated):
// 2160-02-01T00:00:00.000Z

INTERVAL (day to second)

const date = MomentDB.intervalDayToSecond('-47482 12:01:20.903');
const iso = date.toISOString();
// iso output (assuming today is 2030-02-03 and time in UTC is as indicated):
// 1900-02-03T12:01:20.903Z

// can also pass a reference date to use instead of the default current date
const date2 = MomentDB.intervalDayToSecond('-47482 12:01:20.903', new Date(Date.UTC(2030, 1, 3)));
const iso = date.toISOString();
// iso output (assuming today is 2030-02-03 and time in UTC is as indicated):
// 1900-02-03T12:01:20.903Z