最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

show real time clock on html using javascript - Stack Overflow

programmeradmin3浏览0评论

can someone help me with this code, I'm really bad using JS

I want to show only Brazil time in the application, regardless of where the user accesses the application.

<head>
    <script>
        function display_c() {
            var refresh = 1000; // Refresh rate in milli seconds
            mytime = setTimeout('display_ct()', refresh)
        }

        function display_ct() {
            var strcount
            var x = new Date().toLocaleString("en-US", {timeZone: "America/Sao_Paulo"});
            var x1 = x.getMonth() + 1 + "/" + x.getDate() + "/" + x.getYear();
            x1 = x1 + " - " + x.getHours( ) + ":" + x.getMinutes() + ":" + x.getSeconds();
            document.getElementById('ct').innerHTML = x1;

            tt = display_c();
        }
    </script>
</head>

<body onload=display_ct();>
    <span id='ct' ></span>
</body>

Error

painel:48 Uncaught TypeError: x.getMonth is not a function
at display_ct (painel:48)
at onload (painel:57)

can someone help me with this code, I'm really bad using JS

I want to show only Brazil time in the application, regardless of where the user accesses the application.

<head>
    <script>
        function display_c() {
            var refresh = 1000; // Refresh rate in milli seconds
            mytime = setTimeout('display_ct()', refresh)
        }

        function display_ct() {
            var strcount
            var x = new Date().toLocaleString("en-US", {timeZone: "America/Sao_Paulo"});
            var x1 = x.getMonth() + 1 + "/" + x.getDate() + "/" + x.getYear();
            x1 = x1 + " - " + x.getHours( ) + ":" + x.getMinutes() + ":" + x.getSeconds();
            document.getElementById('ct').innerHTML = x1;

            tt = display_c();
        }
    </script>
</head>

<body onload=display_ct();>
    <span id='ct' ></span>
</body>

Error

painel:48 Uncaught TypeError: x.getMonth is not a function
at display_ct (painel:48)
at onload (painel:57)
Share Improve this question edited Apr 1, 2022 at 17:41 masud_moni 1,24818 silver badges34 bronze badges asked May 20, 2018 at 18:56 user8762292user8762292 6
  • 1 setInterval not setTimeout – SayJeyHi Commented May 20, 2018 at 18:59
  • you can use moment js, it's really cool: momentjs.com/docs/#/manipulating/utc-offset – Minh Nguyen Commented May 20, 2018 at 19:02
  • I made the change now and the error continues. – user8762292 Commented May 20, 2018 at 19:03
  • x is not a date-object, but a string, because you use toLocaleString so you could write an additional line like: x = new Date(x); before calling getMonth – Blauharley Commented May 20, 2018 at 19:07
  • @Blauharley Do you know how to make it work? – user8762292 Commented May 20, 2018 at 19:12
 |  Show 1 more comment

2 Answers 2

Reset to default 15

This code will do what you want. Your problem was that you used toLocaleString on your x variable and then tried to use the getMonth() method on it. However, that variable is no longer a date; when you use toLocaleString you turn it into a String. The local string, by default, is formatted almost how you like it. I only had to change a little bit to use the dash in between the date and time instead of the default comma.

var timeDisplay = document.getElementById("time");


function refreshTime() {
  var dateString = new Date().toLocaleString("en-US", {timeZone: "America/Sao_Paulo"});
  var formattedString = dateString.replace(", ", " - ");
  timeDisplay.innerHTML = formattedString;
}

setInterval(refreshTime, 1000);
<p id="time"></p>

    // Inside your Javascript file
    function startTime() {
        var today = new Date();
        var h = today.getHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        m = checkTime(m);
        s = checkTime(s);
        document.getElementById('txt').innerHTML =
        h + ":" + m + ":" + s;
        var t = setTimeout(startTime, 500);
    }
    function checkTime(i) {
        if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
        return i;
    }
    
    <!-- Inside your HTML file -->
    <body onload="startTime()">
         <div id="txt"></div>
    </body>

发布评论

评论列表(0)

  1. 暂无评论