Skip to content

TimezonesModule

The TimezonesModule provides a list of timezones and their corresponding information such as name in the "Area/Location" format, title and offset. It can be used to retrieve timezone data for various purposes such as displaying timezone options in a user interface or converting time between different timezones.

Key Components

The module provides a set of reusable components that can be easily integrated into a NestJS project:

Timezone Entity

The Timezone entity is a data schema that represents a timezone within your application. It typically includes fields such as the timezone name, title, and the UTC offset from Coordinated Universal Time (UTC).

NestJS Timezones Service

A TimezonesService provides programmatic access to the Timezones API. It typically includes methods for retrieving timezone entities.

GraphQL Timezones Resolver

The NestJS TimezonesResolver is a component that handles incoming requests related to timezones and retrieves the relevant data using the TimezonesService. It defines the GraphQL queries for fetching timezones data.

How to install TimezonesModule to my app

To create a TimezonesModule in NestJS, you can follow these steps:

  1. Create a timezones folder in your project's src/modules directory.

  2. Install the required dependencies for TimezonesModule:

  3. Inside the timezones folder, create a timezones.service.ts file and extends BaseTimezonesService from NestKit with the following code:

    ts
    // src/modules/timezones/timezones.service.ts
    
    import { BaseTimezonesService } from '@deeepvision/nest-kit/dist/modules/timezones';
    
    export class TimezonesService extends BaseTimezonesService {}
  4. Inside the timezones folder, create a timezones.resolver.ts file and extends BaseTimezonesResolver with the following code:

    ts
    // src/modules/timezones/timezones.resolver.ts
    
    import { BaseTimezonesResolver, Timezone } from '@deeepvision/nest-kit/dist/modules/timezones';
    import { Resolver } from '@nestjs/graphql';
    
    @Resolver(() => Timezone)
    export class TimezonesResolver extends BaseTimezonesResolver() {}
  5. Inside the timezones folder, create a timezones.module.ts file with the following code:

    ts
    // src/modules/timezones/timezones.module.ts
    
    import { TIMEZONES_SERVICE_TOKEN } from '@deeepvision/nest-kit/dist/modules/timezones';
    import { Module } from '@nestjs/common';
    import { TimezonesResolver } from './timezones.resolver';
    import { TimezonesService } from './timezones.service';
    
    @Module({
      providers: [
        TimezonesService,
        {
          provide: TIMEZONES_SERVICE_TOKEN,
          useExisting: TimezonesService,
        },
        TimezonesResolver,
      ],
      exports: [
        TimezonesService,
        {
          provide: TIMEZONES_SERVICE_TOKEN,
          useExisting: TimezonesService,
        },
      ],
    })
    export class TimezonesModule {}
  6. Finally, import the TimezonesModule into your main AppModule or another module:

    ts
    // src/modules/app.module.ts
    
    import { Module } from '@nestjs/common';
    import { TimezonesModule } from './modules/timezones/timezones.module.ts';
    
    @Module({
      imports: [TimezonesModule],
    })
    export class AppModule {}

    In this code, we are importing the TimezonesModule and adding it to the imports array of our AppModule.

How to get timezone by name

Use the getOneByName() method in other parts of your application to retrieve a timezone by their name in the "Area/Location" format (e.g. America/Los_Angeles).

ts
const timezone = await this.timezonesService.getOneByName('America/Los_Angeles');

Use the getOneByNameOrFail() method to retrieve a timezone by their code or throw an error if the timezone is not found.

ts
const group = await this.timezonesService.getOneByNameOrFail('America/Los_Angeles', ctx);

How to get timezones list

Use the getMany() method in other parts of your application to retrieve a list of timezones.

ts
const [timezones, meta] = await this.timezonesService.getMany({
  filter: {
    ...
  },
  orderBy: TimezonesOrderBy.name_ASC,
});

See TimezonesFilter and TimezonesOrderBy for more information on how to filter and order timezones.

Fetch timezone list with GraphQL

Here's an example GraphQL query to fetch timezones.

Created by DeepVision Software.