View previous topic :: View next topic |
Author |
Message |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Mon Oct 04, 2010 3:20 am Post subject: JavaScript: Date() |
|
|
I have no idea why it won't work.
Here is the HTML file:
Code: | <html>
<head>
<script src="./DateTime.js" type="text/javascript"></script>
</head>
<body onLoad="fnUpdate()">
<form name="form">
<textarea cols="50" name="clock" rows="5">Loading...</textarea>
</form>
</body>
</html> |
Here is the JavaScript:
Code: | function fnTimeZone(amTimeZone, amOffSet, amDaylightSavings)
{
this.DateTime = new Date();
this.TimeZone = amTimeZone;
this.OffSet = amOffSet;
this.DaylightSavings = amDaylightSavings;
}
function fnClockString(amDateTime)
{
var sgBuffer, dtAMPM;
var dtYear = amDateTime.getUTCFullYear();
var dtMonth = amDateTime.getUTCMonth() + 1;
var dtDay = amDateTime.getUTCDate();
var dtHour = amDateTime.getUTCHours();
var dtMinute = amDateTime.getUTCMinutes();
var dtSecond = amDateTime.getUTCSeconds();
dtYear = dtYear.toString();
if(0 <= dtHour && dtHour < 12)
{
dtAMPM = "AM";
} else {
dtAMPM = "PM";
dtHour -= 12;
if(dtHour === 0)
{
dtHour = 12;
}
}
if(dtMinute < 10)
{
dtMinute = "0" + dtMinute;
}
if(dtSecond < 10)
{
dtSecond = "0" + dtMinute;
}
sgBuffer = dtMonth + '/' + dtDay + '/' + dtYear; // dtYear or dtYear.substr(2, 2)
sgBuffer = sgBuffer + ' ' + dtHour + ':' + dtMinute + ':' + dtSecond + ' ' + dtAMPM;
return sgBuffer;
}
function fnUpdate()
{
// Time zones
var ayDateTimes = new Array
(
new fnTimeZone("SFO: ", -8, 1), new fnTimeZone("NYC: ", -5, 1)
);
var otDateTime = new Date();
// Handle daylight savings
var otStartDaylightSavings = new Date(otDateTime.getFullYear(), 3, 1);
while(otStartDaylightSavings.getDay() !== 0)
{
otStartDaylightSavings.setDate(otStartDaylightSavings.getDate() + 1);
}
var otEndDaylightSavings = new Date(otDateTime.getFullYear(), 9, 31);
while(otEndDaylightSavings.getDay !== 0)
{
otEndDaylightSavings.getDate(otEndDaylightSavings.getDate() - 1);
}
var bDaylightSavings;
if(otStartDaylightSavings < otDateTime && otDateTime < otEndDaylightSavings)
{
bDaylightSavings = 1; // True
} else {
bDaylightSavings = 0; // False
}
for(var nDateTimes = 0; nDateTimes < ayDateTimes.length; nDateTimes++)
{
if(ayDateTimes[nDateTimes].DaylightSavings == 1 && bDaylightSavings == 1)
{
ayDateTimes[nDateTimes].OffSet++;
}
}
// Get GMT time then apply offsets
var otGMT = new Date();
for(nDateTimes = 0; nDateTimes < ayDateTimes.length; nDateTimes++)
{
ayDateTimes[nDateTimes].DateTime = new Date(otGMT.getTime() + otGMT[nDateTimes].OffSet * 3600 * 1000);
}
window.document.form.clock.value = ayDateTimes[0].TimeZone + fnClockString(ayDateTimes[0].DateTime);
} |
|
|
Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Tue Oct 05, 2010 11:51 pm Post subject: |
|
|
Anyone?
|
|
Back to top |
|
 |
justa_dude Grandmaster Cheater
Reputation: 23
Joined: 29 Jun 2010 Posts: 891
|
Posted: Wed Oct 06, 2010 4:01 am Post subject: |
|
|
"It doesn't work," is not very descriptive. Which part doesn't work?
|
|
Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Wed Oct 06, 2010 8:29 am Post subject: |
|
|
justa_dude wrote: | "It doesn't work," is not very descriptive. Which part doesn't work? |
It was as descriptive as I can be. The page does not even load up. It stays blanks with the mouse in the loading symbol stage.
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8585 Location: 127.0.0.1
|
Posted: Wed Oct 06, 2010 12:59 pm Post subject: |
|
|
Try properly formatting your HTML page. As in, including the DocType at the top of the page, adjusting the html and header blocks as needed based on the doctype you use and so on.
_________________
- Retired. |
|
Back to top |
|
 |
justa_dude Grandmaster Cheater
Reputation: 23
Joined: 29 Jun 2010 Posts: 891
|
Posted: Wed Oct 06, 2010 5:04 pm Post subject: |
|
|
It's a lot easier to isolate a problem if you start with something that works. You can, however, try to go in reverse by removing things until you get expected output. If you reduce your program to simply writing "Hello, world" in the textarea then the rest will be a lot easier to debug and you will be asking much less of us in terms of trying to study your code.
|
|
Back to top |
|
 |
|