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?

  • 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
    Sep 19 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
    Sep 19 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
    Sep 19 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
    Sep 20 at 1:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.