2

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? :)

Mr. 124
  • 23
  • 4

3 Answers3

2

You mean something like

toLocaleString, toLocaleDateString and/or toLocaleTimeString

const d = new Date(1585740129850);

const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric'};
console.log(d.toLocaleString('de-DE',options),d.toLocaleTimeString('de-DE'))

console.log(
  d.toLocaleDateString(),
  d.toLocaleTimeString('en-US')
)  
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

I have posted it here: https://codepen.io/mxnelles/pen/PoqLyPV

function gd(timestamp) {
  var date = new Date(timestamp * 1000);
  var hours = date.getHours();
  var minutes = "0" + date.getMinutes();
  var seconds = "0" + date.getSeconds();

  var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);

  return formattedTime;
}

$(function() {
  $("#calc").click(function() {
    $("#result").text(gd($("#raw").val()));
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="padding:30px">
  <input type="text" value="1585740248" id="raw">
  <button id="calc">Convert Time</button>
  <div id="result"></div>
</div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Michael Nelles
  • 5,426
  • 8
  • 41
  • 57
  • Thank you! By applying your solution I actually solved my problem and even had an layout option. Perfect :) – Mr. 124 Apr 01 '20 at 12:08
0

Convert:

var utcSeconds = 1585740122250;
var somedate = new Date(utcSeconds);
$('#Window').html(somedate.getUTCSeconds()); // format here

Format:

You can look at :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

var somedate = new Date();

// toLocaleDateString() without arguments depends on the implementation,
// the default locale, and the default time zone
console.log(somedate.toLocaleDateString());
// → "12/11/2012" if run in en-US locale with time zone America/Los_Angeles
xdeepakv
  • 7,835
  • 2
  • 22
  • 32