---
source_url: https://www.pubnub.com/docs/chat/chat-sdk/build/features/utility-methods
title: Utility methods
updated_at: 2026-06-24T11:03:56.804Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Utility methods

Helper methods for working with [PubNub timetokens](https://www.pubnub.com/docs/sdks/javascript/api-reference/misc#time).

## Timetoken to date

Convert a PubNub timetoken (100-nanosecond intervals since January 1, 1970) to a `Date` object for displaying message timestamps in human-readable format.

### Method signature

```ts
static TimetokenUtils.timetokenToDate(timetoken: string | number): Date
```

#### Input

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| timetoken | string | Yes |  | Represents the PubNub timetoken to convert into a `Date` object. |

#### Output

| Type | Description |
| --- | --- |
| `Date` | Converted `Date` object. |

### Sample code

Convert a timetoken value of `16200000000000000` to a human-readable date and time format.

```ts
// import the "TimetokenUtils" class from a module
import { TimetokenUtils } from './TimetokenUtils'

// declare a variable 'timetoken' and assign it a value of a PubNub timetoken
const timetoken = 16200000000000000

// declare a "date" variable and assign it a "Date" object representing the corresponding date and time by calling the "timetokenToDate()" method of the "TimetokenUtils" class, passing the timetoken as a parameter
const date = TimetokenUtils.timetokenToDate(timetoken)

 // log the values of the timetoken and date to the console using the "toDateString" and "toLocaleTimeString" methods to format the date in a human-readable date and time format
console.log(`Timetoken: ${timetoken}`)
console.log(`Date: ${date.toDateString()} ${date.toLocaleTimeString()}`)
```

The output of the method is as follows:

```yaml
Timetoken: 16200000000000000
Date: Mon May 03 2021 8:26:40 PM
```

## Date to timetoken

Convert a `Date` object to a PubNub timetoken for retrieving messages at a specific date and time.

### Method signature

```ts
static TimetokenUtils.dateToTimetoken(date: Date): number
```

#### Input

| Parameter | Description |
| --- | --- |
| `date` *Type: `Date`Default: n/a | Represents the date and time to convert into a PubNub timetoken. |

#### Output

| Type | Description |
| --- | --- |
| `number` | Converted timetoken value. |

#### Errors

If the passed value is not a valid `Date` object, you'll get the exception `The value passed as date is not an instance of Date`. If the passed `Date` object is valid, the method will return a number value representing the corresponding PubNub timetoken for that date and time.

### Sample code

Convert a human-readable date and time, `May 3, 2021 00:00:00 GMT`, to a timetoken.

```ts
// import the "TimetokenUtils" class from a module
import { TimetokenUtils } from './TimetokenUtils'

// declare a "Date" object representing a specific date and time
const date = new Date('May 3, 2021 00:00:00 GMT')

// convert the "Date" object to a PubNub timetoken using the "dateToTimetoken()" method of the "TimetokenUtils" class
const timetoken = TimetokenUtils.dateToTimetoken(date)

// log the values of the date and timetoken to the console
console.log(`Date: ${date.toDateString()} ${date.toLocaleTimeString()} GMT`)
console.log(`Timetoken: ${timetoken}`)
```

```yaml
Date: Mon May 03 2021 12:00:00 GMT+0000 (GMT)
Timetoken: 16200000000000000
```

## Unix timestamp to timetoken

Convert a Unix timestamp (seconds since January 1, 1970) to a PubNub timetoken for [retrieving historical messages](https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/history) within a specific time range.

### Method signature

```ts
static TimetokenUtils.unixToTimetoken(unixTime: string | number): number
```

#### Input

| Parameter | Description |
| --- | --- |
| `unixTime` *Type: `string` or `number`Default: n/a | Represents the Unix timestamp to convert into a PubNub timetoken. |

#### Output

| Type | Description |
| --- | --- |
| `number` | Converted timetoken value. |

#### Errors

If the passed value is not a number (`NaN()`), you'll get the exception `The value passed as unixTime is NaN`. If the number is valid, the `unixToTimetoken()` method multiplies it by 10000 and returns the timetoken value.

### Sample code

Convert a Unix timestamp value of `1620000000` (representing May 3, 2021 at 12:00:00 AM GMT) to a timetoken.

```ts
// import the "TimetokenUtils" class from a module
import { TimetokenUtils } from './TimetokenUtils'

// declare the "unixTime" variable and assign it a value of 1620000000
const unixTime = 1620000000

// call the "unixToTimetoken()" method of the "TimetokenUtils" class, passing in the "unixTime" value as a parameter
// this method converts the Unix timestamp value to a PubNub timetoken and returns it
const timetoken = TimetokenUtils.unixToTimetoken(unixTime)

// log the original Unix timestamp value and the resulting timetoken value to the console
console.log(`Unix time: ${unixTime}`)
console.log(`Timetoken: ${timetoken}`)
```

The output of the method is as follows:

```yaml
Unix time: 1620000000
Timetoken: 16200000000000000
```

## Timetoken to Unix timestamp

Convert a PubNub timetoken to a Unix timestamp for use with systems requiring Unix time format.

### Method signature

```ts
static TimetokenUtils.timetokenToUnix(timetoken: string | number): number
```

#### Input

| Parameter | Description |
| --- | --- |
| `timetoken` *Type: `string` or `number`Default: n/a | Represents the PubNub timetoken to convert into a Unix timestamp. |

#### Output

| Type | Description |
| --- | --- |
| `number` | Converted Unix timestamp value. |

#### Errors

If the passed value is not a number (`NaN()`), you'll get the exception `The value passed as timetoken is NaN`. If the number is valid, the `timetokenToUnix()` method divides the timetoken value by 10000 to obtain the Unix timestamp value.

### Sample code

Convert a timetoken value of `16200000000000000` to a Unix timestamp.

```ts
// import the "TimetokenUtils" class from a module
import { TimetokenUtils } from './TimetokenUtils'

// declare a "timetoken" variable and assign it a value of a PubNub timetoken
const timetoken = 16200000000000000

// declare a "unixTime" variable and assign it a Unix timestamp value by calling the "timetokenToUnix()" method of the "TimetokenUtils" class, passing the timetoken as a parameter
const unixTime = TimetokenUtils.timetokenToUnix(timetoken)

// log the value of "timetoken" and the resulting "unixTime" to the console using the date/time format that is commonly used in computer systems and programming languages
console.log(`Timetoken: ${timetoken}`)
console.log(`Unix time: ${unixTime}`)
```

The output of the method is as follows:

```yaml
Timetoken: 16200000000000000
Unix time: 1620000000
```

## Decrypt messages/files

Manually decrypt a message or file that failed automatic decryption by the [crypto module](https://www.pubnub.com/docs/chat/chat-sdk/learn/access-control#data-security). Requires the original cipher key used for encryption.

### Method signature

```ts
static CryptoUtils.decrypt({ chat, message, decryptor, }: {
    chat: Chat;
    message: Message;
    decryptor: (encryptedContent: string) => TextMessageContent;
}): Message
```

#### Input

| Parameter | Description |
| --- | --- |
| `chat` *Type: `Chat`Default: n/a | Instance of the Chat SDK. |
| `message` *Type: `Message`Default: n/a | Instance of the unsuccessfully decrypted message, which contains the decryption error under the `error` field. |
| `decryptor` *Type: `(encryptedContent: string) => TextMessageContent`Default: n/a | Function that takes an encrypted content as a string and returns the decrypted content as a [TextMessageContent](https://www.pubnub.com/docs/chat/chat-sdk/learn/chat-entities/message). |

#### Output

| Type | Description |
| --- | --- |
| `Message` | Returned [Message object](https://www.pubnub.com/docs/chat/chat-sdk/learn/chat-entities/message) with decrypted content and the `error` field on the Message object set to `undefined` (no decryption error). |

### Sample code

Manually decrypt a message with the cipher key `enigma`.

```ts
// assuming the CryptoUtils class and its decrypt method are defined

// sample message to be decrypted
const encryptedChat = /* ... */; // provide the encrypted content here

// decrypt the message with the cipher key "enigma"
const decryptedMessage = CryptoUtils.decrypt({
  chat: /* ... */,  // provide the chat context
  message: /* ... */,  // provide the message to be decrypted
  decryptor: (encryptedContent) => {
    // assuming "enigma" is the cipher key for decryption
    const cryptoModule = CryptoModule.aesCbcCryptoModule({
      cipherKey: "enigma",
    });

    // perform the decryption logic using cryptoModule
    const decryptedArrayBuffer = cryptoModule.decrypt(encryptedContent) as ArrayBuffer;

    // assuming the decrypted content is a UTF-8 encoded JSON string
    const enc = new TextDecoder("utf-8");
    return JSON.parse(enc.decode(decryptedArrayBuffer));
  },
});

// use the decryptedMessage as needed
console.log(decryptedMessage);
```