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

timezone-utility

v1.6.4

Published

A versatile timezone management package designed for CommonJS, ES Module (ESM), JavaScript, and TypeScript projects.

Downloads

860

Readme

Timezone Utility

A versatile timezone management package designed for CommonJS, ES Module (ESM), JavaScript, and TypeScript projects. It offers a range of features, including timezone listing, retrieving labels and values, region-based filtering, and converting between UTC and other timezones.

Table of Contents

Installation

Install the package using npm:

npm install timezone-utility

Usage

// For ES Module (ESM)
import { TimeZone } from "timezone-utility";

// For CommonJS
const { TimeZone } = require("timezone-utility");

Methods Overview

| Method Name | Description | |-------------|-------------| | list | Returns a list of all available time zones. | | listWithoutLabel | Returns a list of all time zone values without labels. | | listWithoutValue | Returns a list of all time zone labels without values. | | listByRegion | Lists time zones for a specific region. | | getLabelFromValue | Gets the label for a given time zone value. | | getValueFromLabel | Gets the value for a given time zone label. | | getRegions | Returns a list of all available regions. | | convertUTCToTimeZone | Converts a UTC date to a specified time zone. | | convertToUTC | Converts a date-time from a specified time zone to UTC. | | convertBetweenTimeZones | Converts a date-time between two specified time zones. | | getCurrentTimeInTimeZone | Gets the current time in a specified time zone. | | getTimeDifferenceBetweenTimeZones | Calculates the time difference between two time zones. | | convertToISO | Converts a formatted date-time string to ISO format. |

Methods

list

Returns a list of all available time zones.

const timeZones = TimeZone.list();
console.log(timeZones);

Output Example:

[
  { "label": "(UTC+00:00) Africa/Abidjan", "value": "Africa/Abidjan" },
  { "label": "(UTC-05:00) America/New_York", "value": "America/New_York" },
  ...
]

listWithoutLabel

Returns a list of all time zone values without labels.

const timeZoneValues = TimeZone.listWithoutLabel();
console.log(timeZoneValues);

Output Example:

[
  "Africa/Abidjan",
  "America/New_York",
  ...
]

listWithoutValue

Returns a list of all time zone labels without values.

const timeZoneLabels = TimeZone.listWithoutValue();
console.log(timeZoneLabels);

Output Example:

[
  "(UTC+00:00) Africa/Abidjan",
  "(UTC-05:00) America/New_York",
  ...
]

listByRegion

Lists time zones for a specific region.

const timeZonesInRegion = TimeZone.listByRegion('America');
console.log(timeZonesInRegion);

Output Example:

[
  { "label": "(UTC-05:00) America/New_York", "value": "America/New_York" },
  { "label": "(UTC-06:00) America/Chicago", "value": "America/Chicago" },
  ...
]

Parameters:

  • region: string: The region to filter time zones.

getLabelFromValue

Gets the label for a given time zone value.

const label = TimeZone.getLabelFromValue('America/New_York');
console.log(label);

Output Example:

"(UTC-05:00) America/New_York"

Parameters:

  • value: TimeZoneNames: The time zone value.

getValueFromLabel

Gets the value for a given time zone label.

const value = TimeZone.getValueFromLabel('(UTC-05:00) America/New_York');
console.log(value);

Output Example:

"America/New_York"

Parameters:

  • label: string: The time zone label.

getRegions

Returns a list of all available regions.

const regions = TimeZone.getRegions();
console.log(regions);

Output Example:

[
  "Africa",
  "America",
  "Asia",
  "Atlantic",
  "Australia",
  "Europe",
  "Indian",
  "Pacific"
]

convertUTCToTimeZone

Converts a UTC date to a specified time zone.

const converted = TimeZone.convertUTCToTimeZone('2023-10-10T10:00:00', 'America/New_York', { is24Hour: true });
console.log(converted);

Output Example:

"2023-10-10 06:00:00"

Parameters:

  • utcDate: Date | string: The UTC date.
  • targetTimeZone: TimeZoneNames: The target time zone.
  • options: ConvertOptions (optional): Object containing optional formatting options:
    • is24Hour: boolean: Whether to use 24-hour format (default true).
    • dateSeparator: string: The date separator (default -).
    • timeSeparator: string: The time separator (default :).

convertToUTC

Converts a date-time from a specified time zone to UTC.

const converted = TimeZone.convertToUTC('2023-10-10T10:00:00', 'America/New_York', { is24Hour: false });
console.log(converted);

Output Example:

"2023-10-10T14:00:00.000Z"

Parameters:

  • dateTime: Date | string: The date-time to convert.
  • sourceTimeZone: TimeZoneNames: The source time zone.
  • options: ConvertOptions (optional): Object containing optional formatting options:
    • is24Hour: boolean: Whether to use 24-hour format (default true).
    • dateSeparator: string: The date separator (default -).
    • timeSeparator: string: The time separator (default :).

convertBetweenTimeZones

Convert a datetime from one timezone to another and format it in either 12-hour or 24-hour format. You can also specify a custom date separator. By default, the function uses a 24-hour format (is24Hour = true) and the date separator is -.

const originalDate = "2024-10-15T14:00:00Z";
const convertedTime = TimeZone.convertBetweenTimeZones(originalDate, 'America/New_York', 'Asia/Dhaka', {is24Hour: false, dateSeparator: '-', timeSeparator: ':'});
console.log(convertedTime); 

Output Example:

"10/15/2024, 12:00:00"

Parameters:

  • date: string: The date-time string to convert.
  • fromTimeZone: TimeZoneNames: The source timezone.
  • toTimeZone: TimeZoneNames: The target timezone.
  • options: ConvertOptions (optional): Object containing optional formatting options:
    • is24Hour: boolean: Whether to use 24-hour format (default true).
    • dateSeparator: string: The date separator (default -).
    • timeSeparator: string: The time separator (default :).

getCurrentTimeInTimeZone

Returns the current date and time in the specified timezone. The method allows formatting options such as 24-hour or 12-hour format and custom date and time separators.

const currentTimeInNY = TimeZone.getCurrentTimeInTimeZone('America/New_York', { is24Hour: false, dateSeparator: '-', timeSeparator: ':' });
console.log(currentTimeInNY); 

Output Example:

"10-15-2024 08:34:56 AM"

Parameters:

  • targetTimeZone: TimeZoneNames: The target timezone in which you want the current time.
  • options: ConvertOptions (optional): Object containing optional formatting options:
    • is24Hour: boolean: Whether to use 24-hour format (default true).
    • dateSeparator: string: The date separator (default -).
    • timeSeparator: string: The time separator (default :).

getTimeDifferenceBetweenTimeZones

Calculates the time difference between two time zones for a specific date.

const difference = TimeZone.getTimeDifferenceBetweenTimeZones('2023-10-10T10:00:00', 'UTC', 'America/New_York');
console.log(difference);

Output Example:

"+4 hours 0 minutes"

Parameters:

  • date: string: The date string to use for calculation.
  • fromTimeZone: TimeZoneNames: The source time zone.
  • toTimeZone: TimeZoneNames: The target time zone.

convertToISO

Converts a formatted date-time string to ISO format.

const isoString = TimeZone.convertToISO("10th October 2023, 10:00 AM");
console.log(isoString);

Output Example:

"2023-10-10T10:00:00"

Parameters:

  • dateTimeString: string: The date-time string to convert.

License

This project is licensed under the MIT License.

Contributing

We welcome contributions to this package! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.