# @kerasus/server-date

> Make the server's clock available in the browser.

Latest version **4.0.3** (published 2022-12-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @kerasus/server-date
pnpm add @kerasus/server-date
yarn add @kerasus/server-date
bun add @kerasus/server-date
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.3 |
| Published | 2022-12-28 |
| First published | 2022-12-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 28.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | a.esmaeeli |
| Maintainers | krasus |
| Keywords | clock, date, JavaScript, server |

## Links

- npm: https://www.npmjs.com/package/@kerasus/server-date
- Repository: https://github.com/kerasus/server-date
- Issues: https://github.com/kerasus/server-date/issues
- npm.io page: https://npm.io/package/@kerasus/server-date

## Dependencies (1)

- [esm](https://npm.io/package/esm.md) 3.2.25

## Alternatives

- [@js-joda/timezone](https://npm.io/package/@js-joda/timezone.md) — 383.4K weekly downloads
- [chartjs-adapter-moment](https://npm.io/package/chartjs-adapter-moment.md) — 210.8K weekly downloads
- [strftime](https://npm.io/package/strftime.md) — 171.2K weekly downloads
- [vue-flatpickr-component](https://npm.io/package/vue-flatpickr-component.md) — 115.8K weekly downloads
- [timepicker](https://npm.io/package/timepicker.md) — 51.0K weekly downloads

## Recent versions

- 4.0.3 (latest) — 2022-12-28

## README

# Changes

This version has been rewritten and breaks compatibility with the previous API.

# Introduction

server-date makes the server's clock available to the client's web browser in
JavaScript.

You can use it when you want to display the current time but don't trust the
browser's clock to be accurate or to synchronize events for different users of
your web site by syncing them all to the server's clock.

There are two implementations. The simplest one, `serverDate.js`, gets the time
from the server on which the library is hosted by reading the
[Date](https://tools.ietf.org/html/rfc7231#section-7.1.1.2) HTTP response
header. You don't need to make any changes on the server if you use this version
but its precision is limited to seconds because that's what's available in the
header.

If you want millisecond precision, and your server can process PHP, then you can
use the `serverDate.js.php` version which will give you precision on the order
of milliseconds.

## Usage

```JavaScript
import { getServerDate } from "./serverDate.js";

const { date, offset, uncertainty } = await getServerDate();

console.log(`The server's date is ${date} +/- ${uncertainty} milliseconds.`);

// some time in the future

const serverDate = new Date(Date.now() + offset);
```

See `example.html` for a complete example.

# References

- Cristian, Flaviu (1989), "Probabilistic clock synchronization", Distributed
  Computing (Springer) 3 (3): 146–158, DOI:10.1007/BF01784024
- MikeWyatt's answer and Willem Mulder's comment in [Sync JS time between
  multiple devices](http://stackoverflow.com/questions/10585910/sync-js-time-between-multiple-devices)
- Rob W's answer to [How to synchronise a client webpage timer with the server](http://stackoverflow.com/questions/9350928/how-to-synchronise-a-client-webpage-timer-with-the-server)

# Copyright

Copyright 2012 David Braun

---
_Source: https://npm.io/package/@kerasus/server-date · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
