PDA

View Full Version : Javascript Date Help



Eagle
June 30th, 2004, 16:18
OK I need some help with the Javascript date functions. I'm trying to print a clock of the user's current time and then print a clock of the user's current time converted to PST (because this is the location of the game server that the website is for, not that it matters why). The problem is, it works great up until the point that I try to convert the date into a more desireable format after which (for me) it drops 5 hours off the time. Here is the code, I hope its readable on the forums. For a practicle display of how it works, feel free to visit http://www.rebornclan.com

EDIT: Oh I should mention the usage. LocalClock() prints your systems local time. ServerClock(GMT Offset) prints the time where the GMT offset specifies. In this case -480 which is PST.



var tid = 0
var num = 0
var speed = 1000

var clock = new Array()
var offset = new Array()

Date.prototype.customFormat = function() {
var daysShort = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
var monthsShort = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

var hours = this.getHours()
var minutes = this.getMinutes()
var seconds = this.getSeconds()
var day = daysShort[this.getDay()]
var month = monthsShort[this.getMonth()]
var date = this.getDate()
var year = this.getYear()
var dn = "AM"

if (navigator.appName == "Netscape") {
year = year + 1900;
}

if (hours > 12){
dn = "PM"
hours = hours - 12
}

if (hours == 0)
hours = 12
if (minutes <= 9)
minutes = "0" + minutes
if (seconds <= 9)
seconds = "0" + seconds

return day + ", " + month + " " + date + ", " + year + " " + hours + ":" + minutes + ":" + seconds + " " + dn
}



function printClock()
{
for (var count = 0; count < num; count++) {
var currentClock = new Date()

if (offset[count] != 0) {
var tempClock = currentClock.getTime()
tempClock += offset[count]
currentClock.setTime(tempClock)
}

clock[count].date.value = currentClock.customFormat()
}

tid = window.setTimeout("printClock()", speed)
}

function startLocal() {
clock[num] = document.forms["form" + num]
var now = new Date()
offset[num] = -((now.getTimezoneOffset()) * 60 * 1000)
if (num == 0) { tid = window.setTimeout("printClock()", speed) }
num++
}

function startServer(diff) {
clock[num] = document.forms["form" + num]
var now = new Date()

if (now.getMonth() >= 1 || now.getMonth() <= 9) { diff += 60 }

offset[num] = (diff) * 60 * 1000

if (num == 0) { tid = window.setTimeout("printClock()", speed) }
num++
}

function LocalClock()
{
document.write('<FORM name="form' + num + '"><TR><TD class="row1"><SPAN class="postbody"><INPUT type="text" size="34" maxlength="30" style="font-size:10px" class="clockline" name="date" value="Clock: Requires Javascript" /></TD></TR></FORM>')
startLocal()
}

function ServerClock(diff)
{
document.write('<FORM name="form' + num + '"><TR><TD class="row1"><SPAN class="postbody"><INPUT type="text" size="34" maxlength="30" style="font-size:10px" class="clockline" name="date" value="Clock: Requires Javascript" /></TD></TR></FORM>')
startServer(diff)
}

zAlbee
July 1st, 2004, 05:12
i believe the problem lies in that you're assuming the new Date() returns a date in the GMT timezone, but it doesn't. it returns the local time. So in your LocalTime() function, you don't need to compensate for any offset, and in the ServerTime() you need to compensate for both timezones.

Alternatively, it seems you can use the functions getUTCMonth, getUTCYear, getUTCDay, etc. to work the way you want, but that seems kind of a bother :) (plus they're only implemented in Javascript 1.3+).

Eagle
July 1st, 2004, 16:54
Hmm, thanks for the help Zalbee, Ive fixed the normal clock but the server clock doesnt quite work still. Visually I want to convert it to GMT then to PST. In actuallity I will just be calculating the offset based on the user's time zone offset, but I cant seem to get the formula right.