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

tz-local-date

v1.0.2

Published

Fast timezone-aware date comparsion and manipulation

Downloads

1,054

Readme

Timezone Local Date

CI Coverage Status

Fast timezone-aware date comparsion and manipulation.

Features

  • ⚡️ Fast date calculations using native Date objects
  • 🌏 Timezone-aware with support for daylight saving time
  • 🪶 Lightweight with no dependencies

Quickstart

1. Install tz-local-date.

npm install tz-local-date

2. Initialize an instance with the desired timezone.

import { LocalDate } from "tz-local-date";

const ld = new LocalDate("UTC");

3. Start comparing and manipulating dates.

const now = new Date();

const startOfToday = ld.startOfDay(now);
const tomorrow = ld.startOfNextDay(now);

ld.isAfter(tomorrow, startOfToday); // returns true

API Reference

Constructor

LocalDate
LocalDate(timezone: string, reference?: Date | number)

Constructs a new LocalDate instance configured with the given timezone. All calculations and comparisons using the constructed LocalDate instance will normalize given inputs to the configured timezone.
The given timezone must be a valid IANA TZ identifier.

The optional reference parameter determines the date which is used to calculate the offset of the given timezone from UTC. This parameter is useful for timezones with daylight saving time, or has changed since 1 Jan 1970. Defaults to UNIX epoch.

Date Manipulation

LocalDate::startOfDay
startOfDay(date: Date | number): Date

Returns a Date representing the start of the day (i.e. 12 midnight) of the given date.

const ld = new LocalDate("Asia/Singapore");

ld.startOfDay(new Date("2023-01-01T12:34:56+08:00")); // Returns new Date('2023-01-01T00:00:00.000+08:00')

// The date returned is indepdenent of the timezone used to contruct the given Date object.
ld.staryOfDay(new Date("2023-01-01T04:03:02+00:00")); // Returns new Date('2023-01-01T00:00:00.000+08:00')

LocalDate::startOfNextDay
startOfNextDay(date: Date | number): Date

Returns a Date representing the start of the next day of the given date.

const ld = new LocalDate("Asia/Singapore");

ld.startOfNextDay(new Date("2023-01-01T12:34:56+08:00")); // Returns new Date('2023-01-02T00:00:00.000+08:00')

LocalDate::startOfPreviousDay
startOfPreviousDay(date: Date | number): Date

Returns a Date representing the start of the previous day of the given date.

const ld = new LocalDate("Asia/Singapore");

ld.startOfNextDay(new Date("2023-01-01T12:34:56+08:00")); // Returns new Date('2022-12-31T00:00:00.000+08:00')

LocalDate::endOfDay
endOfDay(date: Date | number): Date

Returns a Date representing the end of the day (i.e. 1 millisecond before 12 midnight) of the given date.

const ld = new LocalDate("Asia/Singapore");

ld.endOfDay(new Date("2023-01-01T12:34:56+08:00")); // Returns new Date('2023-01-01T23:59:59.999+08:00')

Date Comparison

LocalDate::isSame
isSame(lhs: Date | number, rhs: Date | number): boolean

Returns true if lhs has the same date as rhs in the configured timezone, false otherwise.

LocalDate::isSameOrBefore
isSameOrBefore(lhs: Date | number, rhs: Date | number): boolean

Returns true if lhs has the same date as or is before rhs in the configured timezone, false otherwise.

LocalDate::isSameOrAfter
isSameOrAfter(lhs: Date | number, rhs: Date | number): boolean

Returns true if lhs has the same date as or is after rhs in the configured timezone, false otherwise.

LocalDate::isBefore
isBefore(lhs: Date | number, rhs: Date | number): boolean

Returns true if the date lhs is strictly before the date of rhs in the configured timezone, false otherwise.
Ignores the time components of lhs and rhs.

LocalDate::isAfter
isAfter(lhs: Date | number, rhs: Date | number): boolean

Returns true if the date lhs is strictly after the date of rhs in the configured timezone, false otherwise.
Ignores the time components of lhs and rhs.

Date Parsing

LocalDate::format
format(date: Date | number, format?: string): string

Returns the given date as a string formatted according to the given string of tokens.
Defaults to YYYY-MM-DD.

Supported Tokens
YYYY: Year, padded to four digits.
YY: Year, truncated to two digits.
M: Month, starting from 1.
MM: Month, starting from 1, padded to two digits.
D: Day of month, starting from 1.
DD: Day of month, starting from 1, padded to two digits.

LocalDate::getComponents
getComponents(date: Date | number): { year: number; month: number; day: number }

Returns the year, month, and day of month of the given date, localized to the configured timezone.

LocalDate::getDay
getDay(date: Date | number): "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday"

Returns the day of the week of the given date, localized to the configured timezone.

LocalDate::getMillisecondsFromStartOfDay
getMillisecondsFromStartOfDay(date: Date | number): number

Returns the number of milliseconds the given date is from the start of the day (i.e. 12 midnight), localized to the configured timezone.

Daylight Saving Time Adjustments

LocalDate::at
at(reference: Date | number): LocalDate

Returns a new LocalDate instance with its reference set to the given date.