Issue with Javascript bringing back incorrect time

J

Jay Dhanji

Hi, I've got a site which uses the javascript below, im using IE 6 SP1
and Windows 2000 SP4, when i launch the html with the javascript, it
brings back the time but doesnt take the DST into consideration. So
basically at the moment, its one hour behind even though in control
panel for DST, it is checked.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>TestTime</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<meta name="GENERATOR" content="Rational Application Developer">
</head>
<body>
<script type="text/javascript">

function Button1_onclick() {
CallBack();
}

function fGetCurrentTime(){
var curDate = new Date();

// Strings to hold bits of time
var curDatehh = curDate.getHours();
curDatehh = curDatehh + "";
var curDatemi = curDate.getMinutes();
curDatemi = curDatemi + "";
if ( curDatehh.length == 1) { curDatehh = "0" + curDatehh }
if ( curDatemi.length == 1) { curDatemi = "0" + curDatemi }

document.getElementById('TimeDisplay').innerText = curDatehh + ":"
+ curDatemi;
}

</script>
<table width="100">
<tr>
<td style="font-size:larger" id="TimeDisplay"><b>NOT SET</
b></td>
</tr>
<tr>
<td>
<input id="UpdateTime" type="button"
value="UpdateTime" language="javascript" onclick="return
fGetCurrentTime();" name="UpdateTime" title="UpdateTime" />
</td>
</tr>
</table>
</body>
</html>
 
V

VK

Hi, I've got a site which uses the javascript below, im using IE 6 SP1
and Windows 2000 SP4, when i launch the html with the javascript, it
brings back the time but doesnt take the DST into consideration. So
basically at the moment, its one hour behind even though in control
panel for DST, it is checked.

Your code as posted doesn't produce any effect: no expected result, no
error (checked on IE8). I didn't study why exactly. This simplified
code works on IE, Fx, Safari, Chrome, Opera under Windows Vista SP2
and it displays correct system time with DST account. Try it on your
computer, if still DST troubles then what timezone are you in exactly?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>TestTime</title>
<meta http-equiv="Content-Type"
content="text/html;charset=ISO-8859-1">
<script type="text/javascript">

var text = (/*@cc_on true || @*/ false) ?
"innerText" : "textContent";

function fGetCurrentTime(){
var curDate = new Date();

// Strings to hold bits of time
var curDatehh = "" + curDate.getHours();
var curDatemi = "" + curDate.getMinutes();

if ( curDatehh.length == 1)
{ curDatehh = "0" + curDatehh; }
if ( curDatemi.length == 1)
{ curDatemi = "0" + curDatemi; }

document.getElementById("TimeDisplay")[text] =
curDatehh + ":" + curDatemi;

}

</script>
</head>
<body>
<table>
<tr>
<td style="font-size:larger" id="TimeDisplay"><b>NOT SET</b></td>
</tr>
<tr>
<td>
<input type="button" id="UpdateTime" name="UpdateTime"
value="UpdateTime"
onclick="return fGetCurrentTime();" />
</td>
</tr>
</table>
</body>
</html>

..
 
D

Dr J R Stockton

In comp.lang.javascript message <0dc9cdcf-9a7f-420d-8fdf-662d4b812fed@h1
3g2000yqm.googlegroups.com>, Thu, 17 Jun 2010 08:12:48, Jay Dhanji
Hi, I've got a site which uses the javascript below, im using IE 6 SP1
and Windows 2000 SP4, when i launch the html with the javascript, it
brings back the time but doesnt take the DST into consideration. So
basically at the moment, its one hour behind even though in control
panel for DST, it is checked.


You need to say also whether the operating system displays the correct
time.

You could also say where you are, and the names of the places chosen on
the list that selects where the computer thinks it is.

You don't need that code ; alert(new Date()) is enough for the test.

Site writers, even if writing for an intranet community forced to use a
single browser, should always have ready access to several browsers for
testing purposes.

The code that you posted does nothing.

That is because you allowed your posting agent to wrap your code,
breaking at least two lines, of which the important one describes the
button's onclick response.

Code posted in News needs to be executable as transmitted; and if
possible also as normally displayed.

After repair, your code shows the correct time here, in Firefox 3.0.19
and MS IE 8 and Opera 10.10.

Checking with the W3 validator http://validator.w3.org/ would have
picked up one instance of line wrap.
 
T

Thomas 'PointedEars' Lahn

Dr said:
Checking with the W3 validator http://validator.w3.org/ would have
picked up one instance of line wrap.

I don't see how. Notice that the OP was using Google Groups for posting,
which is not a good idea to begin with also because automatic line wraps are
only visible there *after* posting (there is no preview feature there).

The markup as posted certainly has several syntax errors, but considering
that non-obvious counter-intuitive automatic wrapping (which breaks the end
tag of the longer line), the _W3C_ Validator would _not_ have found the
remaining wrapped `return' statement in the attribute value: newline is
allowed within HTML attribute values. And since the OP described that there
would be a result, it is not reasonable to assume that line-wrapping was the
cause of the problem.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Jay said:
Hi, I've got a site which uses the javascript below, im using IE 6 SP1
and Windows 2000 SP4, when i launch the html with the javascript, it
brings back the time but doesnt take the DST into consideration. So
basically at the moment, its one hour behind even though in control
panel for DST, it is checked.

You really should not be using or developing for Windows 2000 anymore; SP4
is ca. 7 years, and IE 6 SP1 is ca. 5 years out of date. Support for both
products will end on 2010-07-13:

<http://support.microsoft.com/ph/1131>

(Consequently,) I do not have Windows 2000 anymore (I have only Vista on the
other partition), but I have tried your unwrapped code in IE 6.0.2800.1106
(6 SP1) on Wine 1.1.32 as Windows 98 and could not reproduce your result
(I am using CEST). Perhaps you have used getUTCHours() instead?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

You did not include the system identifier; note that this puts several
layout engines into a compatibility mode (sometimes called Quirks Mode)
which gives you less control over the presentation than Standards
(Compliance) Mode.
<html>
<head>
<title>TestTime</title>

<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<meta name="GENERATOR" content="Rational Application Developer">
</head>
<body>
<script type="text/javascript">

function Button1_onclick() {
CallBack();
}

This function is irrelevant to your problem, you should have removed it from
the posting. Perhaps it is also unused in your original code, where you
should have removed it, too. A good IDE like Eclipse 3.6 (Helios) for
JavaScript Web Developers can spot unused functions and variables for you.

<http://eclipse.org/downloads/packages/eclipse-ide-javascript-web-
developers/heliosr>
function fGetCurrentTime(){
var curDate = new Date();

// Strings to hold bits of time
var curDatehh = curDate.getHours();

OK, except that this is no string value.
curDatehh = curDatehh + "";

Unnecessary, see below.
var curDatemi = curDate.getMinutes();
OK.

curDatemi = curDatemi + "";

Unnecessary, see below.
if ( curDatehh.length == 1) { curDatehh = "0" + curDatehh }
if ( curDatemi.length == 1) { curDatemi = "0" + curDatemi }

It makes more sense to test whether the original numeric value is less than
10. Also consider writing and using a general function that formats
strings. That can simply be leadingZero(…) for a start.
document.getElementById('TimeDisplay').innerText = curDatehh + ":"
+ curDatemi;

The values of `curDatehh' and `curDatemi' are implicitly converted to String
for the purpose of string concatenation here.
}

</script>
<table width="100">

Avoid mixing deprecated format attributes and recommended CSS. By doing
that, you might even be able to declare HTML 4.01 Strict.
<tr>
<td style="font-size:larger" id="TimeDisplay"><b>NOT SET</
b></td>

Avoid mixing stylesheets and presentational elements, too, even if the
latter are not deprecated (like `B').
</tr>
<tr>
<td>
<input id="UpdateTime" type="button"

What do you need the ID for here? Avoid unnecessary IDs, especially in
IE/MSHTML where they become names of properties of a host object in the
scope chain.
value="UpdateTime" language="javascript" onclick="return

There is no `language' attribute for the `input' element.
fGetCurrentTime();" name="UpdateTime" title="UpdateTime" />

This is HTML (not XHTML); lose the ` /'.

<http://validator.w3.org/>

See also <http://jibbering.com/faq/#posting>


HTH

PointedEars

P.S.: The pronoun is always spelled `I' in English.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top