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 - Find the block number closest to a given date or timestamp
Prerequisites
- Node.js v18+ installed
- A Moralis API key (get one free)
Step 1: Set Up Your Project
Create a new directory for your project and initialize it:Step 2: Create the Script
Create a file calledindex.js and add the following code:
YOUR_API_KEY with your actual Moralis API key.
Step 3: Run the Script
Execute the script to find the block:Example Response
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 - Fetch transaction details
- Get Wallet History - Get wallet transaction history

