Ethereum Block By Date
Get Ethereum block number by a given date, using viem
Installation
bun i eth-block-for-date
Usage
import EthDater from 'eth-block-for-date';
import { createPublicClient, http } from 'viem';
const client = createPublicClient({ transport: http('YOUR_RPC_URL') });
const dater = new EthDater(client);
let block = await dater.getDate(
'2016-07-20T13:20:40Z',
true,
false
);
Requests
let block = await dater.getDate(
'2016-07-20T13:20:40Z',
true,
false
);
let blocks = await dater.getEvery(
'weeks',
'2019-09-02T12:00:00Z',
'2019-09-30T12:00:00Z',
1,
true,
false
);
let requests = dater.requests;
Note: if the given date is before the first block date in the blockchain, the script will return 1 as block number. If the given date is in the future, the script will return the last block number in the blockchain.
Moment.js
The package uses moment.js plugin to parse dates. Read more about valid dates and time zones in the plugin's documentation: Moment.js
Demo
Explore the online demo of this package at blockanddate.com, crafted by 0xAskar.
Examples
Every first block of the year:
let blocks = await dater.getEvery('years', '2016-01-01T00:00:00Z', '2019-01-01T00:00:00Z');
Every last block of the year:
let blocks = await dater.getEvery('years', '2016-01-01T00:00:00Z', '2019-01-01T00:00:00Z', 1, false);
Every first block of every 4 hours of October 10, 2019:
let blocks = await dater.getEvery('hours', '2019-10-10T00:00:00Z', '2019-10-11T00:00:00Z', 4);
Need Help
If you need any help, please contact me via GitHub issues page: GitHub
Donations
If you like my package and you want to support the development or buy me a cup of coffee, you could donate to me via Ethereum: 0x18F54b91f7e19c51fA701E7ed5628fA45441d872
Thanks ❤️