Ethereum Block By Date
Get Ethereum block number by a given date. Or blocks by a given period duration.
Works with any Ethereum based mainnet or testnet networks.
Installation
Use npm:
npm i block-by-date-ethers
Or Yarn:
yarn add block-by-date-ethers
Usage
const EthDater = require('block-by-date-ethers');
const dater = new EthDater(
provider
);
let block = await dater.getDate(
'2016-07-20T13:20:40Z',
true
);
let blocks = await dater.getEvery(
'weeks',
'2019-09-02T12:00:00Z',
'2019-09-30T12:00:00Z',
1,
true
);
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
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);