PDA

View Full Version : Converting UTC Time to other time zones


rik408
02-13-2003, 08:26 PM
Hello,
I was wondering if someone could give me a hand with this. I believe
I have obtained the correct UTC (GMT, Greenwich) time, but I am
having some difficulty getting it to convert to Hong Kong and New
York times.
Hong Kond is + 8 hours, and New York is - 5 hours,
Anyway, here is the code. I have included the HTML to make copy-n-
paste easier.
--
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-
1">
</HEAD>
<BODY>

<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
var time = new Date();
var hour = time.getUTCHours();
var minute = time.getUTCMinutes();
var day = time.getUTCDay();
var wd;
var apm = "am";
var dn = new Array
("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Sa
turday")
var london = hour; // London Time = CMT Time: UTC -0 Hours
var hkt = hour+ 8; //Hong Kong Time = UTC +8 Hours
var nyt = hour - 5; //New York Time = UTC -5 Hours
wd = dn[day];
day += 10;
if (hour > 11) apm = "pm";
if (minute <= 9) minute = "0" + minute;
if (hour > 12) hour = hour - 12;
if (hour == 0) hour = 12;
if (day == 1) day += "st"
if (day == 2) day += "nd"
if (day == 3) day += "rd"
if (day >= 4 && day <= 20) day += "th"
if (day == 21) day += "st"
if (day == 22) day += "nd"
if (day == 23) day += "rd"
if (day >= 24 && day <= 30) day += "th"
if (day == 31) day += "st"
var CMT;
CMT = hour + ":" + minute + " " + apm + " ¤ " + wd + ", " + day;
var CMT2 = CMT = hour + minute +":" + " " + apm + "
<BIG>•</BIG> " + day + ", " ;
var CMT3 = hour + ":" + minute + " " + apm + " <BIG>:</BIG> " + wd
+ " " + "the " +day;
var hk = hkt + ":" + minute + " " + apm + " <BIG>:</BIG> " + wd
+ " " + "the " +day;
var ny = nyt + ":" + minute + " " + apm + " <BIG>:</BIG> " + wd
+ " " + "the " +day;
document.write("London: " +CMT3);
document.write("<BR>");
document.write("Hong Kong: " +hk);
document.write("<BR>");
document.write("New York: " +ny);
//document.theClock.theTime.value =ny; //write to text area
// -->
</SCRIPT>

<BR>
<BR>
<form name="theClock">
<input type=text name="theTime" size=8>
</form>

</BODY>
</HTML>
--

Here are the results I obtained:

London: 7:37 pm : Thursday the 14th
Hong Kong: 27:37 pm : Thursday the 14th
New York: 14:37 pm : Thursday the 14th

I believe London is ok, but HK should be "Friday" by now, and there
is no such time as "27:37" (even on a 24-hour clock!). I wanted all
in AM/PM format. As you can see, New Yourk's time reverted back to a
24 hour clock as well.

Also, I wanted all 3 results to write to a text box, but again, I am
having problems.

If anyone could assist, they would have my appreciation.

Rick

Owl
02-14-2003, 12:21 AM
Hi Rick,<BODY>
<BR>
<form name="theClock">
London: <input type=text name="london" size=27><br><br>
Hong Kong: <input type=text name="hongkong" size=27><br><br>
New York: <input type=text name="newyork" size=27>
</form>

<SCRIPT LANGUAGE="JAVASCRIPT">
dn = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

function formatUTC(h){
var time = new Date();
time.setHours(time.getHours()+h)
var hour = time.getUTCHours();
var minute = time.getUTCMinutes();
var day = time.getUTCDay();
var wd = dn[day];
var date = time.getUTCDate();
var apm = "am";
if (hour > 11) apm = "pm";
if (minute <= 9) minute = "0" + minute;
if (hour > 12) hour = hour - 12;
if (hour == 0) hour = 12;
if (date == 1) date += "st"
if (date == 2) date += "nd"
if (date == 3) date += "rd"
if (date >= 4 && date <= 20) date += "th"
if (date == 21) date += "st"
if (date == 22) date += "nd"
if (date == 23) date += "rd"
if (date >= 24 && date <= 30) date += "th"
if (date == 31) date += "st"

return " " + hour + ":" + minute + " " + apm + " : " + wd + " " + "the " +date;
}

with(document.theClock) {
london.value = formatUTC(0)
hongkong.value = formatUTC(8)
newyork.value = formatUTC(-5)
}
</SCRIPT>

</BODY> ( •) (• )
>>V

rik408
02-14-2003, 12:45 AM
Wow, great job!
I am getting better at this, but its still a bit confusing at times
One more thing -- how horrible would it be to have the different time zone outputs to write to a text field [via 3 onClick Buttons -- one onClick for *each* time zone)]

Again, thanks so much!

Rick