Java Reference
In-Depth Information
simply changes the hours to 12 and adds one day to the date stored inside the Date object. You use this
to your benefit in the following line:
now.setMinutes(now.getMinutes() + now.getTimezoneOffset() +
parseInt(offset, 10) + dstAdjust);
Let's break down this line to see how it works. Suppose that you're in New York, with the local summer
time of 5:11, and you want to know what time it is in Berlin. How does your line of code calculate this?
First, you get the minutes of the current local time; it's 5:11, so now.getMinutes() returns 11 .
Then you get the difference, in minutes, between the user's local time and UTC using
now.getTimezoneOffset() . If you are in New York, which is different from UTC by 4 hours during the
summer, this is 240 minutes.
Then you get the integer value of the time difference between the standard winter time in the selected city
and UTC time, which is stored in offset . You've used parseInt() here because it's one of the few situations
where JavaScript gets confused and assumes you want to join two strings together rather than treat the
values as numbers and add them together. Remember that you got offset from an HTML element's value,
and that an HTML element's values are strings, even when they hold characters that are digits. Because you
want the time in Berlin, which is 60 minutes different from UTC time, this value will be 60 .
Finally, you add the value of dstAdjust . Because it's summer where you are and Berlin uses daylight
savings hours, this value is 60 .
So you have the following:
11 + 240 + 60 + 60 = 371
Therefore, now.setMinutes() is setting the minutes to 371 . Clearly, there's no such thing as 371
minutes past the hour, so instead JavaScript assumes you mean 6 hours and 11 minutes after 5:00, that
being 11:11—the time in Berlin that you wanted.
Finally, the updateTime() function creates the resultsText variable and then writes the results to the
divCityTime :
var resultsText = selectedCity + " time is " +
now.toLocaleString();
document.getElementById("divCityTime").innerHTML = resultsText;
}
html5 form objeCt properties and methods
HTML4 was finalized in 1997, and it wasn't until 2012 that the web community saw a push
for HTML5. Needless to say, HTML hadn't seen a significant update until the introduction of
HTML5. So for fifteen years, web developers have worked with form controls that grossly don't
meet developers' and users' needs. Thankfully, that changes with HTML5.
 
Search WWH ::




Custom Search