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

date-immutable

v1.0.1

Published

A super simple immutable date object, with builders, operations, destructuring, enumeration and month starting from 1.

Downloads

23

Readme

DateImmutable

Introduction

Immutable date object with:

  • Month start from 1
  • Builders
  • Getters
  • Destructuring
  • Enumeration
  • Immutable operations

Installation

npm install date-immutable
yarn add date-immutable

Basic usage

import { DateImmutable } from 'date-immutable'

const date = DateImmutable.from(2023, 5, 4) // May 4th, 2023, 00:00:00.000 local time
const anotherDate = date.add({ month: 1 }) // June 4th, 2023, 00:00:00.000 local time

Builders

import { DateImmutable } from 'date-immutable'

// (locale time) year: number, month: number, date = 1, hours = 0, minutes = 0, seconds = 0, milliseconds = 0
DateImmutable.from(2023, 5, 4, 9, 41, 0, 0) // Also accepts string, timestamp, Date or object

// String, @see https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
DateImmutable.fromString('2023-05-04T09:41:00.000Z')

// Javascript Date
DateImmutable.fromDate(new Date('2023-05-04T09:41:00.000Z'))

// UNIX timestamp: number
DateImmutable.fromTimestamp(1683193260000)

// Object { year?: number, month?: number, date?: number, hours?: number, minutes?: number, seconds?: number, milliseconds?: number }
DateImmutable.fromObject({ year: 2023, month: 5, date: 4, hours: 9, minutes: 41, seconds: 0, milliseconds: 0 })

Getters

import { DateImmutable } from 'date-immutable'

const date = DateImmutable.from(2023, 5, 4, 9, 41)
date.year // 2023
date.month // 5
date.date // 4
date.day // 0
date.hours // 9
date.minutes // 41
date.seconds // 0
date.milliseconds // 0
date.timezoneOffset // your local timezone offset

Destructuring

import { DateImmutable } from 'date-immutable'

const { year, month, date, day, hours, minutes, seconds, milliseconds, timezoneOffset } = DateImmutable.from(2023, 5, 4, 9, 41)

Enumeration

Caution: day and timezoneOffset are not part of the enumeration to match the fromObject builder or the toObject signature.

import { DateImmutable } from 'date-immutable'

const initialDate = DateImmutable.from(2023, 5, 4, 9, 41)
const nextYear = DateImmutable.from({ ...initialDate, year: initialDate.year + 1 })
// {
//   year: 2024,
//   month: 5,
//   date: 4,
//   hours: 9,
//   minutes: 41,
//   seconds: 0,
//   milliseconds: 0,
// }

Immutable operations

import { DateImmutable } from 'date-immutable'

const initialDate = DateImmutable.from(2023, 5, 4, 9, 41)

// { years?: number; months?: number; days?: number; hours?: number; minutes?: number; seconds?: number; milliseconds?: number }
const nextYear = initialDate.add({ years: 1 })
// {
//   year: 2024,
//   month: 5,
//   date: 4,
//   hours: 9,
//   minutes: 41,
//   seconds: 0,
//   milliseconds: 0,
// }

// Same as `add`
const previousYear = initialDate.substract({ years: 1 })
// {
//   year: 2024,
//   month: 5,
//   date: 4,
//   hours: 9,
//   minutes: 41,
//   seconds: 0,
//   milliseconds: 0,
// }

// { year?: number; month?: number; date?: number; hours?: number; minutes?: number; seconds?: number; milliseconds?: number }
const anotherYear = initialDate.set({ year: 2025 })
// {
//   year: 2025,
//   month: 5,
//   date: 4,
//   hours: 9,
//   minutes: 41,
//   seconds: 0,
//   milliseconds: 0,
// }

Converters

import { DateImmutable } from 'date-immutable'

const date = DateImmutable.from(/* … */)
date.toTimestamp() // Same as Date.getTime()
date.toString() // Same as Date.toString()
date.toISOString() // Same as Date.toISOString()
date.toJSON() // Same as Date.toJSON()
date.toDate() // Build a new Date object
date.toObject() // { year, month, date, hours, minutes, seconds, milliseconds }