2 Replies - 973 Views - Last Post: 15 June 2011 - 06:04 AM

#1 colinpmasters  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-June 11

Help with Clock Coding in Actionscript

Posted 15 June 2011 - 01:44 AM

Hi Guys n Girls, this is my first time posting a coding question so please be gentle!

I am building a clock using Flash/Actionscript to display multiple time zones, offset against UTC and adjusting for Daylight Saving for the timezones that adhere to it. My problem is that one of the time zones is Mumbai, which is 5.5 hours ahead of UTC, and which incidentally doesn't adhere to DST.

Here's some code;

time=new Date(); // time object

//set the time
var seconds = time.getUTCSeconds()
var minutes = time.getUTCMinutes()
var hours = time.getUTCHours()

//set the date
var day = time.getUTCDate()
var month = time.getUTCMonth()
var year = time.getUTCFullYear()
var dst;

//sets the daylight saving rule
if((day>=13 && month>=3) && (day<=6 && month<=11)){
	dst = true;
}

//Adds zeros to single figures
if(minutes<10)
{
minutes = "0" + minutes;
}
if(seconds<10)
{
seconds = "0" + seconds;
}



Hopefully that explains the background, now what happens is I output the time to a dynamic text box like this;


if (hours<19){
			mumbai.text = (hours +5.5) + ":" + minutes + ":" + seconds;
		}
		else{
			mumbai.text = (hours -19) + ":" + minutes + ":" + seconds;
		}



Now, this is obviously wrong as my clock outputs something like 18.5:00:00 instead of 18:30:00, am I making sense?

My first solution would be to add 30 to the minute count;


mumbai.text = (hours +5) + ":" + (minutes + 30) + ":" + seconds;




but this obviously adds 30 minutes to the minutes all the time, and doesn't add an hour when the minutes are greater than 30.

I would appreciate some advice, I know the answer is probably staring me in the face, but I can't see it!!!

Thanks in advance!
Colin

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Clock Coding in Actionscript

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Help with Clock Coding in Actionscript

Posted 15 June 2011 - 01:58 AM

One thing you can do is check if the decimal part exists and add it accordingly.

Suppose :

var x=5.5;
var dec= x - Math.floor(x); // now dec contains 0.5;
mumbai.text = (hours +Math.floor(x)) + ":" + minutes+dec*60 + ":" + seconds;



But thats not all. It may happen that minutes+dec*60 might exceed 59. In that case you have to reset minutes to 0 and add 1 to hours. So keep a check on that too.
Was This Post Helpful? 0
  • +
  • -

#3 colinpmasters  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-June 11

Re: Help with Clock Coding in Actionscript

Posted 15 June 2011 - 06:04 AM

Thanks dude, you really helped focus me onto where the problem arose, and I managed a workaround as follows;

Excuse the longer code, this is my first time coding in Actionscript and i'm finding it a little erm, well not very nice. I created two functions, one to calculate the correct hour and one to calculate the correct minutes;

function mumbaiHours(){
	var mumbaiHours = hours;
	
	if (hours<19){
		if (minutes>29){
			mumbaiHours = hours +6;
		}
		else {
			mumbaiHours = hours +5;
		}
	}
	else {
		if (minutes>29){
			mumbaiHours = hours -18;
		}
		else {
			mumbaiHours = hours -19;
		}
	}
	if (mumbaiHours <10){
		mumbaiHours = "0" + mumbaiHours;
	}
	return mumbaiHours;
}
function mumbaiMinutes(){
	var mumbaiMinutes = 0;
	
	if (minutes>29){
		mumbaiMinutes = minutes -30;
	}
	else {
		mumbaiMinutes = minutes +30;
	}
	if (mumbaiMinutes <10){
		mumbaiMinutes = "0" + mumbaiMinutes;
	}
	return mumbaiMinutes;
}



Then, I called these functions as follows;

mumbai.text = mumbaiHours() + ":" + mumbaiMinutes() + ":" + seconds;



This seems to work, but also seems like a lot of coding. It was the 59 minutes comment that made me think of this solution, any thoughts?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1