猿问

将 <div> 元素内的数字输入转换为人类可读的日期

我正在尝试将纪元时间代码转换为人类可读的结构。我在 stackoverflow 上读到以下代码可以工作:链接

var utcSeconds = 1234567890;

var d = new Date(0); // The 0 there is the key, which sets the date to the epoch

d.setUTCSeconds(utcSeconds);

我在 html 文档内的脚本标记内调用的函数中使用了这种方法,但不知何故我没有以正确的方式执行此操作。我缺少什么?


<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>

对于上下文:我正在调用以太坊上的智能合约。智能合约返回以太坊的“现在”值,它是纪元格式的整数,含义如下:1585727288。通过显示一个长数字,我想显示一个易于阅读的日期格式。我怎样才能做到这一点?:)


婷婷同学_
浏览 124回答 3
3回答

烙印99

你的意思是像toLocaleString、 toLocaleDateString和/或toLocaleTimeStringconst 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'))  

喵喔喔

我已将其发布在这里: https:&nbsp;//codepen.io/mxnelles/pen/PoqLyPVfunction gd(timestamp) {&nbsp; var date = new Date(timestamp * 1000);&nbsp; var hours = date.getHours();&nbsp; var minutes = "0" + date.getMinutes();&nbsp; var seconds = "0" + date.getSeconds();&nbsp; var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);&nbsp; return formattedTime;}$(function() {&nbsp; $("#calc").click(function() {&nbsp; &nbsp; $("#result").text(gd($("#raw").val()));&nbsp; })})<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div style="padding:30px">&nbsp; <input type="text" value="1585740248" id="raw">&nbsp; <button id="calc">Convert Time</button>&nbsp; <div id="result"></div></div>

DIEA

转变:var utcSeconds = 1585740122250;var somedate = new Date(utcSeconds);$('#Window').html(somedate.getUTCSeconds()); // format here格式:您可以查看:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateStringvar somedate = new Date();// toLocaleDateString() without arguments depends on the implementation,// the default locale, and the default time zoneconsole.log(somedate.toLocaleDateString());// → "12/11/2012" if run in en-US locale with time zone America/Los_Angeles
随时随地看视频慕课网APP

相关分类

Html5
我要回答