Options
All
  • Public
  • Public/Protected
  • All
Menu

@pinemach/truncate-date

Coverage Status Build Status NPM version MIT License

@pinemach/truncate-date is a small JavaScript package with a single concern: truncating datetime inputs to remove all precision past a given time unit.

You can read the full API documentation at pineapplemachine.github.io/truncate-date-js/.

Installation

You can install this package with the package manager of your choice. For example,

npm install @pinemach/truncate-date

You can then import and use the module like so:

const truncateDate = require("@pinemach/truncate-date").truncateDate; // CommonJS
import {truncateDate} from "@pinemach/truncate-date"; // ES6 modules

Usage

This package exports the truncateDate function, which accepts a Date object or other time value input and a time unit to truncate that value to.

Truncation occurs in the UTC timezone.

import {truncateDate} from "@pinemach/truncate-date";

const myDate = new Date("2020-04-15T12:30:15.123Z");

// Logs "2020-04-15T12:30:15.000Z"
console.log(truncateDate("second", myDate));

// Logs "2020-04-15T12:00:00.000Z"
console.log(truncateDate("hour", myDate));

// Logs "2020-04-15T00:00:00.000Z"
console.log(truncateDate("day", myDate));

// Logs "2020-01-01T00:00:00.000Z"
console.log(truncateDate("year", myDate));

Index

Type aliases

TruncateDateUnit

TruncateDateUnit: "millisecond" | "second" | "minute" | "hour" | "day" | "month" | "year"

The truncateDate function accepts one of these values as its "unit" input.

TruncateDateValue

TruncateDateValue: string | number | Date | object | object

Represents the types which the truncateDate function will accept as a "value" input.

Variables

Const TruncateDateUnitErrorMessage

TruncateDateUnitErrorMessage: "Unrecognized time unit." = "Unrecognized time unit."

Error message produced when calling truncateDate with an unrecognized time unit argument.

See TruncateDateUnit for a list of recognized time units.

Const TruncateDateValueErrorMessage

TruncateDateValueErrorMessage: "Unrecognized time value." = "Unrecognized time value."

Error message produced when calling truncateDate with an unrecognized time value argument.

See TruncateDateValue for a list of recognized value types.

Functions

truncateDate

  • This function can be used to truncate a date so that any precision beyond the specified unit is lost. Dates are truncated in the UTC timezone.

    Parameters

    • unit: TruncateDateUnit

      Remove all precision beyond this unit. For example, when the unit is "hour", the minutes, seconds, and milliseconds will all be set to zero. In this case the hour, day, month, and year will remain unchanged.

    • value: TruncateDateValue

      The date value which should be truncated. The function accepts one of these value types:

      • Numeric value representing a number of milliseconds since Unix epoch.
      • ISO 8601 timestamp string or other string input recognized by the JavaScript Date constructor.
      • A JavaScript Date instance.
      • A moment date object.
      • A Day.js date object.
      • A luxon date object.

      When the input represents an invalid date, so will the output be an invalid date.

    Returns Date

    The truncated time value, represented as a JavaScript Date instance.

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc