0

What is the most simple way to get hour of the day in Solidity? Will this work? for example creating a condition that works only when it's past 17:00 during that day?

if(hour > 17) { //yay this is it }

Will this work?

4
  • Know the time it's an "real world" information so that you will need to use oracles for that. I've never tried before, but you can take a look on here: github.com/pipermerriam/ethereum-datetime
    – JuliSmz
    Commented Sep 19, 2021 at 22:55
  • there is block.timestamp which is seconds since a date in 1970 i reckon another solution would be uint8((timestamp / 60 / 60) % 24); but nto sure if this is correct
    – smenir443
    Commented Sep 19, 2021 at 23:26
  • I think thats a bad idea to start with, dealing with time that way in solidity seems way too complicated to me (time zones, leap seconds... have to be accounted for and i dont see any way of doing it). If i were you i would rethink my contract design
    – Foxxxey
    Commented Sep 19, 2021 at 23:45
  • Take care and analyse which way requires more computational power 'cause the transaction cost. You can try the 2 ways on remix or something similar...
    – JuliSmz
    Commented Sep 20, 2021 at 1:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.