Utility methods
This section lists various helper methods that provide a convenient way to work with PubNub timetokens.
Timetoken to date
The timetokenToDate()
method of the TimetokenUtils
class converts a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970) to a Date
object representing the corresponding date and time.
Use this method when you want to display the timetoken of each message or event in the chat history in a human-readable format.
Method signature
static TimetokenUtils.timetokenToDate(timetoken: string | number): Date
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
timetoken | string or number | Yes | n/a | Represents the PubNub timetoken to convert into a Date object. |
Output
Type | Description |
---|---|
number | Converted Date object. |
Basic usage
Convert a timetoken value of 16200000000000000
to a human-readable date and time format.
// 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:
Timetoken: 16200000000000000
Date: Mon May 03 2021 8:26:40 PM
Date to timetoken
The dateToTimetoken()
method of the TimetokenUtils
class converts the Date
object representing the corresponding date and time into a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970).
Use this method to represent a specific date and time as a PubNub timetoken value, for example, to retrieve messages from a PubNub channel at a particular date and time.
Method signature
static TimetokenUtils.dateToTimetoken(date: Date): number
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
date | Date | Yes | 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.
Basic usage
Convert a human-readable date and time, May 3, 2021 00:00:00 GMT
, to a timetoken.
// 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}`)
Date: Mon May 03 2021 12:00:00 GMT+0000 (GMT)
Timetoken: 16200000000000000
Unix timestamp to timetoken
The unixToTimetoken()
method of the TimetokenUtils
class converts a Unix timestamp (a number of seconds since January 1, 1970) to a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970).
Use this method when you need a timetoken to retrieve historical messages with a specific timestamp range from Message Persistence.
Method signature
This method takes the following parameters:
static TimetokenUtils.unixToTimetoken(unixTime: string | number): number
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
unixTime | string or number | Yes | 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.
Basic usage
Convert a Unix timestamp value of 1620000000
(representing May 3, 2021 at 12:00:00 AM GMT) to a timetoken.
// 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:
Unix time: 1620000000
Timetoken: 16200000000000000
Timetoken to Unix timestamp
The timetokenToUnix()
method of the TimetokenUtils
class converts a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970) to a Unix timestamp (a number of seconds since January 1, 1970).
Use this method to convert PubNub timetoken for use in another context or system that requires a Unix timestamp.
Method signature
static TimetokenUtils.timetokenToUnix(timetoken: string | number): number
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
timetoken | string or number | Yes | 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.
Basic usage
Convert a timetoken value of 16200000000000000
to a Unix timestamp.
// 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:
Timetoken: 16200000000000000
Unix time: 1620000000