# How to calculate with time and dates?

What's the best way to calculate with dates in solidity, in particular, getting the total number of days between the start date and end date?

Also

What's the best way to get the current date/time in solidity?

Much appreciated.

• What's the best way to calculate with dates in solidity, in particular, getting the total number of days between the start date and end date?

Date are stored in the Unix epoch format (timestamp) which consists in an integer of the number of seconds since 1970-01-01.

If you want to compare two dates, it's simple math. For example:

``````uint startDate = 1514764800; // 2018-01-01 00:00:00
uint endDate = 1518220800; // 2018-02-10 00:00:00

uint diff = (endDate - startDate) / 60 / 60 / 24; // 40 days
``````
• What's the best way to get the current date/time in solidity?

You only need to use the keyword `now` [more info]

• How do you calculate the timestamp? Jan 21, 2018 at 18:32
• Depending of the language you use to interact with the smart contract. In JavaScript, you can do: `Math.floor(Date.now() / 1000)`. Jan 21, 2018 at 18:36
• @GregJeanmart Hi, looking on this forum I found your answer. I am in a similar situation and I want to ask you if you can help me with my problem. In my dapp, the contract offers a service that makes a refund for hotel bookings. More precisely, if some weather conditions (which I recover with an oracle) are not respected then the refund is triggered. For example: I start my holiday in the place where I booked, but if after one or two days the weather conditions do not respect the conditions of the contract then I get a refund. Jan 27, 2021 at 1:13
• @GregJeanmart My problem is when does the check start? I was thinking of inserting a structure in the smart contract containing the request id, client, start date, end date, city. Then I create a list of structures. Periodically the smart contract iterates on the list and compares the current date with the start dates of all the reservations in the list and if there is a match it starts the weather check. Is there a way to tell the smart contract to iterate periodically? Jan 27, 2021 at 1:13
• @GregJeanmart If so, how can I compare the current date (how do I get it? In version 0.7.0, the alias now was removed) with the start date? Does it make sense to think of the system like this or to avoid all this I delegate this verification of the dates to the backend? I mean, the backend checks when the booking starts and at that point asks the smart contract to carry out the weather check. I'm so sorry for the length of the message Jan 27, 2021 at 1:13

Within the Ethereum Virtual Machine, there isn't a way to get the current time, other than the block time.

To get current block time stamp we can directly use block.timestamp in solidity. Its returns a time in unix timestamp.

Ethereum provides Time unites to facility date computation to a particular level, refer the following document. https://solidity.readthedocs.io/en/develop/units-and-global-variables.html#time-units

Few examples: Following method returns the timestamp of 30 days before.

``````function getTime () public view returns(uint256 time){
return block.timestamp - 30 days;
}
``````

To compare validity (LastTimeStamp is variable contains old timestamp)

``````function checkExpiry() public view returns(bool success){

if (LastTimeStamp +30 days >= block.timestamp)
{
return true;
}
else
{
return false;
}
}
``````