---
metadata:
  - name: generator
    content: Diplodoc Platform v5.66.0
alternate:
  - https://yandex.com/dev/games/doc/en/sdk/sdk-server-time.md
  - https://yandex.com/dev/games/doc/hi/sdk/sdk-server-time.md
  - https://yandex.com/dev/games/doc/ko/sdk/sdk-server-time.md
  - https://yandex.com/dev/games/doc/ru/sdk/sdk-server-time.md
  - https://yandex.com/dev/games/doc/tr/sdk/sdk-server-time.md
  - https://yandex.com/dev/games/doc/vi/sdk/sdk-server-time.md
  - https://yandex.com/dev/games/doc/zh/sdk/sdk-server-time.md
  - href: https://yandex.com/dev/games/doc/en/sdk/sdk-server-time.md
    type: text/markdown
    title: Markdown version
  - href: https://yandex.com/dev/games/doc/en/llms.txt?revision=r21993288
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/dev/games/doc/en/llms.txt?revision=r21993288

# Server time

<!-- source: en/_includes/script-common.md -->
<!-- source: en/_includes/script/index-js.md -->

<!-- endsource: en/_includes/script/index-js.md -->

<!-- source: en/_includes/script/requirements-js.md -->

<!-- endsource: en/_includes/script/requirements-js.md -->

<!-- source: en/_includes/script/image-modal-js.md -->

<!-- endsource: en/_includes/script/image-modal-js.md -->
<!-- endsource: en/_includes/script-common.md -->

Yandex Games SDK allows you to get time synchronized with the server. The method is useful for:

<!-- source: en/_includes/sdk/common-with-plugins.md -->
- **Cheat protection**: users will not be able to influence game processes by changing the time on their device.
- **Game events**: based on it, you can add activities and rewards that require a trusted time source (for example, daily or weekly bonuses, seasonal events, and quests).
<!-- endsource: en/_includes/sdk/common-with-plugins.md -->

## ysdk.serverTime() {#server-time}

This method returns a `timestamp`, the server time in milliseconds, the same for all devices. It is similar to `Date.now()` in the format of the result, however, the latter returns the user's device time, which may differ from server time and is not protected against cheating by players. The `ysdk.serverTime()` method is resistant to tampering with the device's system time, making it more reliable.

Call it every time you need to get the current time.

{% list tabs %}

- Using await

    ```javascript showLineNumbers
    const ysdk = await YaGames.init();

    // Returns time in ms, synchronized with the server.
    ysdk.serverTime(); // For example, 1720613073778.

    // Call it again after some time.
    ysdk.serverTime(); // For example, 1720613132635.
    ```

- Without using await

    ```javascript showLineNumbers
    YaGames.init()
        .then((ysdk) => {
            // Returns time in ms, synchronized with the server.
            ysdk.serverTime(); // For example, 1720613073778.

            // Some time later, call it again.
            ysdk.serverTime(); // For example, 1720613132635.
        });
    ```

{% endlist %}

## Examples of Implementing Daily Rewards {#reward-examples}

- `ysdk.serverTime()` is used to get reliable server time.
- Data is saved using [player.setData()](https://yandex.com/dev/games/doc/en/sdk/sdk-player.md#ingame-data).
- Duplicate reward claiming is prevented.
- The time is compared in a secure way.

{% note warning %}

The `giveReward()` function in the examples is your own implementation of granting a reward to the player.

{% endnote %}

### Reward 24 hours after the last game visit {#twenty-four-hours-reward}

```javascript showLineNumbers
YaGames.init()
    .then(async (ysdk) => {
        // Player initialization.
        const player = await ysdk.getPlayer();

        // Get saved data.
        const data = await player.getData();

        // Current server time.
        const currentTime = ysdk.serverTime();

        // Time when the last reward was received (if not set, use 0).
        const lastRewardTime = data.lastRewardTime || 0;

        // 24 hours in milliseconds.
        const DAY_IN_MS = 24 * 60 * 60 * 1000;

        if (currentTime - lastRewardTime >= DAY_IN_MS) {
            // More than 24 hours have passed — the reward can be given.
            await giveReward(); // Your function for awarding a reward.

            // Save the new reward claim time.
            await player.setData({
                lastRewardTime: currentTime
            });
        }
    });
```

### Reward is given once per calendar day (reset at midnight UTC) {#calendar-day-reward}

```javascript showLineNumbers
YaGames.init()
    .then(async (ysdk) => {
        // Player initialization.
        const player = await ysdk.getPlayer();

        // Get saved data.
        const data = await player.getData();

        // Current server time.
        const currentTime = ysdk.serverTime();

        // Get the date of the last reward in "YYYY-MM-DD" format.
        const lastRewardDate = data.lastRewardDate || '';

        // Get the current date in "YYYY-MM-DD" format.
        const currentDate = new Date(currentTime).toISOString().split('T')[0];

        if (currentDate !== lastRewardDate) {
            // The reward hasn't been claimed yet today.
            await giveReward(); // Your function for awarding a reward.

            // Save the reward claim date.
            await player.setData({
                lastRewardDate: currentDate
            });
        }
    });
```

---

<!-- source: en/_includes/sdk/sdk-support.md -->
{% note info %}

Support staff help you publish a ready-made game on the Yandex Games platform. For applied questions about development and testing, other developers will answer in the [Discord community](https://discord.com/invite/wU4p3whr4T){.external}.

{% endnote %}

If you encounter a problem or have a question while using the Yandex Games SDK, contact support:

<!-- source: en/_includes/button-chat.md -->
<a href="https://yandex.com/chat/#/user/a4fa5c06-75db-9b38-6eea-b1673785f7d5">
  <span class="button">Write to chat</span>
</a>
<!-- endsource: en/_includes/button-chat.md -->
<!-- endsource: en/_includes/sdk/sdk-support.md -->
