0

thanks for taking the time to read this.

I'm having a bit of trouble handling time in Solidity. I want the owner of a contract to be able to set a deadline in days (which will be used in the rest of the contract) on the constructor. I wrote this:

constructor(uint _price, uint _days) public {
    owner = msg.sender;       
    price = _price;
    deadlineInDays = now + _days;
}

But as you may be aware, when the owner inputs any value in _days, it is added to the Epoch Time format (now). So, for example, if I set it to "10", when I call this function to return it:

function getPriceAndDeadline() public view returns (uint _price, uint _deadline) 
{
    return (price, deadlineInDays);
}    

It returns something like this:

_price 1000
_deadline 1544005222 // this is only ten seconds after I ran the constructor

I obviously wanted it to return whatever "now" is plus "_days" in days, not in seconds.

I tried this (below), but it doesn't work.

constructor(uint _price, uint _days) public {
    owner = msg.sender;       
    price = _price;
    deadlineInDays = now + _days days;
}

Any ideas on how to handle this?

I appreciate any inputs in advance! Cheers!

1 Answer 1

1

Use

deadlineInDays = now + _days * 1 days;

It will be working.

1
  • Thanks a lot!, it worked like a breeze! Such a concise and elegant solution! Dec 5, 2018 at 11:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.