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
|
Show 1 more comment
2 Answers
Reset to default 15This 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>
setInterval
notsetTimeout
– SayJeyHi Commented May 20, 2018 at 18:59moment js
, it's really cool: momentjs.com/docs/#/manipulating/utc-offset – Minh Nguyen Commented May 20, 2018 at 19:02x
is not a date-object, but a string, because you usetoLocaleString
so you could write an additional line like:x = new Date(x);
before callinggetMonth
– Blauharley Commented May 20, 2018 at 19:07