how to get human readable timestamp through solidity ? I know there is block.timestamp to get the unix time. But i want it in human readable format like Mon Jun 19 11:51:19 IST 2017.

Please suggest.

  • Why would you want that? can you give more context? Because for me human readable format would be use to display the date for an user which can be done by the interface which interact with your contract (like a website for instance) – Crema Jun 19 '17 at 10:38
  • I want to fetch Block creation time. If i call the same via javascript, i will get different time. i want to call it so that i can know when the data block has been added to the chain. – Amit Kumar Jun 19 '17 at 12:22
  • 1
    I would like to get unix time like 1460714400 in human readable format like Mon Jun 19 11:51:19 IST 2017 (or any human readable format). Please help – Amit Kumar Jun 19 '17 at 12:29

I can't really think of a use case for this within an Ethereum contract. When you talk about human readable formats, it seems to me like you're looking to make a user interface.

I would recommend looking into Truffle. It's an excellent framework for building web3 applications, which are essentially just websites that can talk to contracts on the Ethereum blockchain. Then you can just make a web3 call to get block.timestamp in seconds, and use normal JavaScript to convert it to a human readable format and display it in a browser like Google Chrome, using extensions like Metamask.

For conversion from seconds since the Unix epoch to a readable format with JavaScript, see: https://stackoverflow.com/questions/5416920/timestamp-to-human-readable-format

  • Thanks Eric for the help. However there are multiple use case in which knowing the block creation time can be helpful. For Example, When a police complaint is made. Wealthy and politically strong people frequently make normal public suffer by manipulating the complaint. However in this case a person can know when his block has been updated(via fetching block creation/updating time) – Amit Kumar Jun 19 '17 at 13:28
  • 1
    I agree that block creation time is useful, but you're misunderstanding my point. Formatting the block time into a readable format should be done from the user interface, which can reference the block times on the Ethereum blockchain, without running on Ethereum itself – user9402 Jun 19 '17 at 13:35
  • 2
    Look into projects like Truffle, Metamask, Mist, and ITFS. The idea is that you can build websites that are run decentrally where no single party has control. So you can use normal frontends written in HTML, JavaScript, and CSS, but the backend logic is handled by Ethereum – user9402 Jun 19 '17 at 13:38
  • 2
    Thanks Eric. Cant we directly convert the same unix time fetched via block.timestamp in smart contract in any function call in the same contract. I am new to ethereum though, and i can see that it is quite similar to Javascript. Provide your feedback please. – Amit Kumar Jun 19 '17 at 13:43
  • 1
    Currently there is no easy way to do so, nor is there any need. And Solidity merely has similar syntax to JavaScript. But in the background it's compiling to EVM bytecode, which is completely different then JavaScript – user9402 Jun 19 '17 at 13:49

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