Skip to content

Date conversion ​

To allow for easier manipulation of date and time, the SDK exports the DateTime class which is a wrapper around the built-in Date class. Below we will go over the methods of instantiation, utility functions and time formats.

Internally the transactions and other time/date assets are encoded using the TAI64 format. We return a DateTime class, to allow of easier conversion and formatting between the two formats.

Instantiating a DateTime ​

We have a host of static method for instantiation of our DateTime class.

ts
import { DateTime } from 'fuels';

const tai64: DateTime = DateTime.fromTai64('4611686020108779339');
const unixSeconds: DateTime = DateTime.fromUnixSeconds(1681391398);
const unixMilliseconds: DateTime = DateTime.fromUnixMilliseconds(1681391398000);
See code in context

TAI64 ​

fromTai64 is a static method, that allows the creation of DateTime class from a TAI64 string.

toTai64 is an instance method, that allows the conversion of a DateTime class to a TAI64 string.

ts
import { DateTime } from 'fuels';

const date: DateTime = DateTime.fromTai64('4611686020108779339');
console.log(date.toIso); // "4611686020108779339"

const tai64: string = date.toTai64();
console.log(tai64); // "4611686020108779339"
See code in context

UNIX ​

fromUnixMilliseconds is a static method, that allows the creation of DateTime class from a UNIX Milliseconds number.

toUnixMilliseconds is an instance method, that allows the conversion of a DateTime class to a UNIX number in milliseconds.

ts
import { DateTime } from 'fuels';

const date: DateTime = DateTime.fromUnixMilliseconds(1681391398000);

const unixMilliseconds: number = date.toUnixMilliseconds();
console.log(unixMilliseconds); // 1681391398000
See code in context

fromUnixSeconds is a static method, that allows the creation of DateTime class from a UNIX Seconds number.

toUnixSeconds is an instance method, that allows the conversion of a DateTime class to a UNIX number in seconds.

ts
import { DateTime } from 'fuels';

const date: DateTime = DateTime.fromUnixSeconds(1681391398);

const unixSeconds: number = date.toUnixSeconds();
console.log(unixSeconds); // 1681391398
See code in context

Date ​

The DateTime class extends the functionality of the Date object, so all method are available for your usages.

ts
import { DateTime } from 'fuels';

const dateTime: DateTime = DateTime.fromUnixMilliseconds(1681391398000);

// Extends the Date object
const date: Date = dateTime;

// Date object methods
date.getTime(); // 1681391398000
date.toISOString(); // 2023-04-13T13:09:58.000Z
date.toDateString(); // Thu Apr 13 2023
See code in context

Formats ​

Here we will go over the different date/time formats that we use in the SDK. Internally the blockchain uses the TAI64 format, but we also support the UNIX format for ease of use.

UNIX Format ​

UNIX time is the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, minus leap seconds. Every day is treated as if it contains exactly 86400 seconds, so leap seconds are ignored.

TAI Format ​

TAI stands for Temps Atomique International and is the current international real-time standard Source.

We use TAI64 is a 64-bit integer representing the number of nanoseconds since the epoch.

  • the TAI second beginning exactly (2^62 - s) seconds before the beginning of 1970 TAI, if s is between 0 inclusive and 2^62 exclusive; or

  • the TAI second beginning exactly (2^62 + s) seconds after the beginning of 1970 TAI, if s is between -2^62 inclusive and 0 exclusive.

Source