Skip to main content

Get block by date

GEThttps://deep-index.moralis.io/api/v2/dateToBlock

Get the closest block given the date.

QUERY PARAMS
chainstring
The chain to query
datestringrequired
Unix date in milliseconds or a datestring (any format that is accepted by momentjs)
Responses
API KEY
import Moralis  from 'moralis';
import { EvmChain } from '@moralisweb3/evm-utils';

try {
const chain = EvmChain.ETHEREUM;

// unix date
cons date = '123..'

await Moralis.start({
apiKey: 'YOUR_API_KEY',
// ...and any other configuration
});

const response = await Moralis.EvmApi.block.getDateToBlock({
date,
chain,
});

console.log(response?.result);
} catch (e) {
console.error(e);
}

Response Example
{
"date": "2020-01-01T00:00:00+00:00",
"block": 9193266,
"timestamp": 1577836811,
"block_timestamp": "2022-01-03T22:59:39.000Z",
"hash": "0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171",
"parent_hash": "0x011d1fc45839de975cc55d758943f9f1d204f80a90eb631f3bf064b80d53e045"
}