Consider we need to determine an expiry date for a contract such that after this date the contract will be destroyed as follows:
uint256 public expiryDate;
constructor(uint256 duration)
public
payable
{
owner= msg.sender;
expiryDate = now + duration;
}
function destryContract() public {
require(now >= expiryDate);
selfdestruct(owner);
}
Now, consider we need to calculate the exact duration for this expiry date, for example, we want to distroy the contract exactly after one month.
How to calculate the value of the duration
variable as an integer
?
now
specifies the number of seconds elapsed since 1/1/1970. Therefore, you should pass the duration in seconds. It cannot be done accurately for one month, because this period of time is not defined accurately (i.e., it can be anything between 28 - 31 days). If you're OK with the approximation of one month as 30 days, then you can use60 * 60 * 24 * 30
.expiryDate = now() + dayToWait*1 days
, so that afterwards you can compare expiryDate and now directly. Maybe if you want to spare a multiplication in solidity and the related gas cost it is better the @goodvibration 's approach ^^