I see a lot of discussion about date and time, timestamp... But they don't answer to my question: How can i convert from calendar format into timestamp format for comparing datetime?

e.g: I have this smart contract:

pragma solidity >=0.4.21 <0.6.0;

contract User {

    address userWalletAddr;
    string userName;
    string userEmail; //The start date of the production

    struct UserActivity{
        address userWalletAddr;
        string activityDescription;
        uint dateTime;

    UserActivity[] private activities;

    constructor(string  memory _userName, string memory _userEmail) public {
        userWalletAddr = msg.sender;
        userName = _userName;
        userEmail = _userEmail;

    function addUserActivity(string  memory _activityDescription) public {
                    userWalletAddr : msg.sender,
                    activityDescription: _activityDescription,
                    dateTime: now

    //Retrieve activities of a user according to an intervall of time
    function listUserActivities(address _userWalletAddr, uint _startInterVal, uint _endInterval)
        public view returns(address, string memory,  uint){
        for(uint i = 0; i <= activities.length; i ++){
                activities[i].userWalletAddr == _userWalletAddr &&
                activities[i].dateTime >= _startInterVal &&
                activities[i].dateTime < _endInterval)

Now, i want to retrieve a list of activities of a user according an intervall [ _startInterVal, _endInterval] like indicated in the function listUserActivities().

Your help would please me

  • 1
    Can you define what "calendar format" and "timestamp format" mean to you? And what, if anything, do you not like about the current code you have? – user19510 Aug 21 '19 at 9:32
  • Calendar format is appear like 08/21/2019 @ 12:00pm (UTC) that equivalent to 1566388800, the timestamp format(the number of seconds) since Jan 01 1970. (UTC). Now, in my case, i want to retrieve a list of activities of a user according an intervall [ _startInterVal, _endInterval] that i will indicate in a User Interface. e.g: The list of activities registered from 08/15/2019 @ 12:00pm to (UTC) 08/21/2019 @ 12:00pm (UTC). – Mongetro Goint Aug 21 '19 at 10:09
  • 1
    Well, you would do this conversion in your UI (maybe JavaScript?), so start your search there. moment.js comes to mind as a good library for parsing dates. – user19510 Aug 21 '19 at 10:38

Quite simple.

Smart contract only saves UNIX timestamp.

You probably execute the Contract via JavaScript.

There you can create a Timestamp of current second with:


If you want to convert:

new Date('2012.08.10').getTime() / 1000

In case this doesn't work you need to cut the string using substr()

Then you can call it like this:

new Date(year, month, day, hours, minutes, seconds, milliseconds).getTime() / 1000

Your Answer

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

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