How to get the first block of the day?
Closed this issue · 4 comments
KennethAshley commented
Thanks for the great package. It's been working great. I am curious though how to get the first block of the day? Thanks!
monosux commented
Hi,
I'm glad you like my package!
To get the first block of the day you just need to know the necessary date/time. For example, to get today's first block you can use this code:
let block = await dater.getDate('2020-07-22T00:00:00Z');
Keep in mind the timezones!
monosux commented
How to get every block in the past 24 hours?
To get it you can use a function like that:
const getBlocks = async () => {
// Get the date/time 24 hours ago
const yesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));
// Get the block by this time
const first_block = await dater.getDate(yesterday);
// Get current block
const last_block = await web3.eth.getBlockNumber();
// Get all blocks between first and last
let blocks = [];
for (var i = first_block.block; i <= last_block; i++) blocks.push(i);
// Return them 🖖
return blocks;
}
KennethAshley commented
@monosux sweet! thanks for the help!
monosux commented
sweet! thanks for the help!
No problem