Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@google-cloud/precise-date

googleapis9.3mApache-2.05.0.0TypeScript support: included

A simple utility for precise-dateing functions and classes.

nano, date, time, precise

readme

Google Cloud Platform logo

@google-cloud/precise-date

A simple utility for precise-dateing functions and classes.

Installing the package

It's unlikely you will need to install this package directly, as it will be installed as a dependency when you install other @google-cloud packages.

$ npm install --save @google-cloud/precise-date

Using the package

PreciseDate extends the native Date object, so you can use it in place of that or when you need nanosecond precision.

const {PreciseDate} = require('@google-cloud/precise-date');
const date = new PreciseDate('1547253035381101032');

date.toISOString();
// => 2019-01-12T00:30:35.381101032Z

date.toFullTimeString();
// => '1547253035381101032'

API

PreciseDate([time])

Returns a new date instance.

time

Type: string BigInt Object<string, number> [number, number]

// from a full ISO string
date = new PreciseDate('2019-02-08T10:34:29.481145231Z');

// from a string representing nanoseconds
date = new PreciseDate('1549622069481320032');

// from a BigInt representing nanoseconds (requires Node >= 10.7)
date = new PreciseDate(1549622069481320032n);

// from an object containing `seconds` and `nanos` values
date = new PreciseDate({seconds: 1549622069, nanos: 481320032});

// from a tuple representing [seconds, nanos]
date = new PreciseDate([1549622069, 481320032]);

PreciseDate.parseFull(time)

Similar to Date.parse(), but this accepts the same nanosecond time options as the PreciseDate constructor and returns a string representing the nanoseconds in the specified date according to universal time.

PreciseDate.parseFull('2019-02-08T10:34:29.481145231Z');
// => '1549622069481145231'

PreciseDate.fullUTCString(...dateFields)

Similar to Date.UTC(), but also accepts microsecond and nanosecond parameters. Returns a string that represents the number of nanoseconds since January 1, 1970, 00:00:00 UTC.

dateFields

Type: ...number

PreciseDate.fullUTCString(2019, 1, 8, 10, 34, 29, 481, 145, 231);
// => '1549622069481145231'

PreciseDate.fullUTC(...dateFields)

Like PreciseDate.fullUTCString() but returns a native BigInt instead of a string. Requires Node >= 10.7.

dateFields

Type: ...number

PreciseDate.fullUTC(2019, 1, 8, 10, 34, 29, 481, 145, 231);
// => 1549622069481145231n

date

PreciseDate instance.

date.getFullTimeString()

Returns a string of the specified date represented in nanoseconds according to universal time.

date.getFullTime()

Like date.getFullTimeString() but returns a native BigInt instead of a string. Requires Node >= 10.7.

date.getMicroseconds()

Returns the microseconds in the specified date according to universal time.

date.getNanoseconds()

Returns the nanoseconds in the specified date according to universal time.

date.setMicroseconds(microseconds)

Sets the microseconds for a specified date according to universal time. Returns a string representing the nanoseconds in the specified date according to universal time.

microseconds

Type: number

date.setNanoseconds(nanoseconds)

Sets the nanoseconds for a specified date according to universal time. Returns a string representing the nanoseconds in the specified date according to universal time.

nanoseconds

Type: number

date.setFullTime(time)

Sets the time to the number of supplied nanoseconds since January 1, 1970, 00:00:00 UTC. Returns a string representing the nanoseconds in the specified date according to universal time (effectively, the value of the argument).

time

Type: number string BigInt

date.toStruct()

Returns an object representing the specified date according to universal time. Refer to google.protobuf.Timestamp for more information about this format.

const {seconds, nanos} = date.toStruct();

date.toTuple()

Like date.toStruct() but returns the seconds and nanos as a tuple.

const [seconds, nanos] = date.toTuple();

Versioning

This library follows Semantic Versioning.

Contributing

Contributions welcome! See the Contributing Guide.

License

Apache Version 2.0

See LICENSE

changelog

Changelog

npm history

5.0.0 (2025-03-03)

⚠ BREAKING CHANGES

  • migrate to Node 18 (#265)

Miscellaneous Chores

4.0.0 (2023-08-10)

⚠ BREAKING CHANGES

  • upgrade to Node 14 (#235)

Miscellaneous Chores

3.0.1 (2022-08-23)

Bug Fixes

3.0.0 (2022-06-07)

⚠ BREAKING CHANGES

  • update library to use Node 12 (#215)

Build System

2.0.4 (2021-09-09)

Bug Fixes

  • build: switch primary branch to main (#186) (aaa4044)

2.0.3 (2020-07-24)

Bug Fixes

  • move gitattributes files to node templates (#130) (d4de5dd)

2.0.2 (2020-07-06)

Bug Fixes

2.0.1 (2020-05-08)

Bug Fixes

2.0.0 (2020-03-26)

⚠ BREAKING CHANGES

  • dep: upgrade typescript/gts (#97)
  • dep: deprecate Node 8 to 10 (#92)

Miscellaneous Chores

1.0.3 (2019-12-05)

Bug Fixes

  • deps: pin TypeScript below 3.7.0 (c4aef9c)

1.0.2 (2019-11-15)

Bug Fixes

  • docs: add jsdoc-region-tag plugin (#60) (a975b1c)

1.0.1 (2019-06-27)

Bug Fixes

  • docs: make anchors work in jsdoc (#43) (5828db8)

1.0.0 (2019-05-02)

Build System

  • upgrade engines field to >=8.10.0 (#25) (10b9cc1)

BREAKING CHANGES

  • upgrade engines field to >=8.10.0 (#25)

v0.1.0

02-19-2019 12:27 PST

Hello, world!

  • feat: initial code commit (#1)

Documentation

  • docs: update links in contrib guide (#4)

Internal / Testing Changes

  • chore: add missing boilerplate files (#3)
  • chore: add dummy docs-test script and fix tests for Node 11 (#6)