I'm trying to convert the epoch time code into a humanreadable structure. I read here on stackoverflow that the following code could work: link
var utcSeconds = 1234567890;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(utcSeconds);
I'm using this approach inside a function that I call inside my script tag inside my html document but somehow I don't do it the right way. What am I missing?
<html>
<head></head>
<body>
<div class="wrapper">
<div class="box window" id="Window"></div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/web3-min-js@1.0.0/web3.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
<script>
var contract;
$(document).ready(function() {
var web3 = new Web3(window.web3.currentProvider)
var contractAddress = "address";
var abi = [abi];
var contract = new web3.eth.Contract(abi, contractAddress);
contract.methods.viewNewestTimestamp().call().then(function(data2) {
var utcSeconds = data2;
var d = new Date(data2);
$('#Window').html(d.utcSeconds(data2));
})
})
</script>
</body>
</html>
For context: I'm calling an smart contract on ethereum. The smart contract returns the "now" value of ethereum which comes as a integer in epoch format means something like this: 1585727288. By instead of showing a long number I want to display a nicely readable date format. How can I do that? :)