> ## Documentation Index
> Fetch the complete documentation index at: https://docs.moralis.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Find the Closest Block by Unix Timestamp

> Learn how to find the block number closest to any given date or timestamp using the Moralis API.

## Introduction

In this tutorial, you'll learn how to find the block number that was mined closest to a specific date or Unix timestamp. This is useful for querying historical blockchain data at specific points in time, such as getting token balances or prices at a particular date. We'll use the following Moralis API endpoint:

* [Get Block by Date](/data-api/evm/blockchain/block-by-date) - Find the block number closest to a given date or timestamp

## Prerequisites

* Node.js v18+ installed
* A Moralis API key ([get one free](https://admin.moralis.io))

## Step 1: Set Up Your Project

Create a new directory for your project and initialize it:

```bash theme={null}
mkdir get-block-by-date && cd get-block-by-date
npm init -y
```

## Step 2: Create the Script

Create a file called `index.js` and add the following code:

```javascript theme={null}
// index.js
const API_KEY = 'YOUR_API_KEY';

async function getBlockByDate() {
  // Find the block closest to January 1, 2024
  const date = '2024-01-01T00:00:00Z';

  const response = await fetch(
    `https://deep-index.moralis.io/api/v2.2/dateToBlock?chain=eth&date=${encodeURIComponent(date)}`,
    {
      headers: {
        'X-API-Key': API_KEY,
      },
    }
  );

  const data = await response.json();
  console.log(JSON.stringify(data, null, 2));
}

getBlockByDate();
```

Replace `YOUR_API_KEY` with your actual Moralis API key.

## Step 3: Run the Script

Execute the script to find the block:

```bash theme={null}
node index.js
```

## Example Response

```json theme={null}
{
  "block": 18908895,
  "date": "2024-01-01T00:00:11.000Z",
  "timestamp": 1704067211,
  "block_timestamp": "2024-01-01T00:00:11.000Z",
  "hash": "0x..."
}
```

## Understanding the Response

| Field             | Description                                    |
| ----------------- | ---------------------------------------------- |
| `block`           | The block number closest to the requested date |
| `date`            | ISO timestamp of when the block was mined      |
| `timestamp`       | Unix timestamp of the block                    |
| `block_timestamp` | Same as `date`, in ISO format                  |
| `hash`            | The block hash                                 |

## Next Steps

* [Get Transaction by Hash](/get-started/tutorials/data-api/blocks-and-transactions/get-a-blockchain-transaction-by-hash) - Fetch transaction details
* [Get Wallet History](/get-started/tutorials/data-api/wallets-and-accounts/get-full-wallet-transaction-history-eoa-and-smart-accounts) - Get wallet transaction history
