I have a clock that displays the month, day, time (i.e. Jul. 23, 21:34p) but is
set to another time zone. However, with my current code the time is on a 24hr
clock and i need it to be on a 12hr one. Plus I need the time change to also be
reflected in the day and month rather than just the time. any idea how to do
this????
var dayText:String;
var dateText:String;
var monthText:String;
var AmPm:String;
_root.onEnterFrame = function() {
var myDate:Date = new Date();
var min:Number = myDate.getMinutes();
//the +12 is the current time plus 12 hours to be the new timezone
var hour:Number = myDate.getHours()+12;
var day:Number = myDate.getDay();
var date:Number = myDate.getDate();
var month:Number = myDate.getMonth();
min = min<10 ? "0"+min : min;
//AM/PM
if (hour>12) {
hour = hour-12;
AmPm = "p";
} else {
AmPm = "a";
}
//day
{
dateText = date+"";
}
//month
if (month == 0) {
monthText = "Jan";
} else if (month == 1) {
monthText = "Feb";
} else if (month == 2) {
monthText = "Mar";
} else if (month == 3) {
monthText = "Apr";
} else if (month == 4) {
monthText = "May";
} else if (month == 5) {
monthText = "Jun";
} else if (month == 6) {
monthText = "Jul";
} else if (month == 7) {
monthText = "Aug";
} else if (month == 8) {
monthText = "Sep";
} else if (month == 9) {
monthText = "Oct";
} else if (month == 10) {
monthText = "Nov";
} else if (month == 11) {
monthText = "Dec";
}
//display
//dateDisplay.text = hour+":"+min+ AmPm + "-" + monthText+"."+ dateText; };
dateDisplay.text = monthText+". "+ dateText+ ", "+ hour+":"+min+ AmPm; };
Noelbaland - 24 Jul 2008 12:16 GMT
Hi there,
All that was needed to get the 24 to a 12 hour clock was to subtract 12 from
the getHours() property.
Below is a script I created that shows just about every date method you can
use. It's set to a 12 hour clock rotation and includes adding a suffix for the
date and the leading 0 for minutes and seconds. Also it uses arrays and loops
instead of if/else statements.Have a look at it and adjust it to your script.
Hope it helps
// Array to hold our month names
var months:Array = new Array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December");
// Array to hold our day of the week names
var days:Array = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");
// Function to loop thru the months array and pull
// out and return the right month name
function get_month(num:Number)
{
for(var i=0; i<months.length; i++){
if(i == num){
var mon = months[i];
return mon;
}
}
}
// Function to loop thru the days array and pull
// out and return the right day of the week name
function get_days(num:Number)
{
for(var i=0; i<days.length; i++){
if(i == num){
var day = days[i];
return day;
}
}
}
// Attaches the leading 0 to seconds and minutes
// for numbers from 0 - 9
function get_zeroes(num:Number)
{
if(num < 10){
var min = num.toString();
min = "0" + min;
} else {
min = num;
}
return min;
}
// Returns the date and the correct suffix for that date
function get_date(num:Number)
{
// Default
var suffix:String = "th";
if(num == 1 || num == 21 || num == 31){
suffix = "st";
}
if(num == 2 || num == 22){
suffix = "nd";
}
if(num == 3 || num == 23){
suffix = "rd";
}
// Concatenates the date and suffix
var return_date = num + suffix;
return return_date;
}
_root.onEnterFrame = function()
{
var my_date:Date = new Date();
var hour:Number = my_date.getHours();
var min:Number = my_date.getMinutes();
var day:Number = my_date.getDay();
var date:Number = my_date.getDate();
var month:Number = my_date.getMonth();
var year:Number = my_date.getFullYear();
var seconds:Number = my_date.getSeconds();
var ampm:String;
var hour_12:Number;
var date_txt:String;
// This is the main check for the 12 - 24 hour difference
// It subtracts 12 from the getHours() property and returns
// the number in a new variable(hour_12). It also determines
// the AM/PM suffix
if(hour > 12){
hour_12 = hour-12;
ampm = "pm";
} else {
hour_12 = hour;
ampm = "am";
}
date_txt = get_days(day)+" "+get_date(date)+" "+get_month(month)+", "+year+"
"+hour_12+":"+get_zeroes(min)+":"+get_zeroes(seconds)+" "+ampm.toUpperCase();
// Textfield on stage that displays date/time
tf.text = date_txt;
}
welling1977 - 24 Jul 2008 14:55 GMT
Thanks so much for posting this. However, I'm trying to get it to work and
nothing is happening. I think I'm doing something wrong with creating my
dynamic text box and possibly naming it incorrectly--
I named the instance name date_txt (I also tried naming the var instance to
date_txt)
I'll keep playing with it. Thanks again!!!
welling1977 - 24 Jul 2008 16:06 GMT
Duh! i just figured it out! add +12
var hour:Number = my_date.getHours()+12;
thanks!!
welling1977 - 24 Jul 2008 16:40 GMT
What is strange is that when I use it in my current fla., I only get "Thursday
24th"
and nothing more.
Everything appears as it should with your code in another file. Any ideas? I
do have some actionscript working at another place, but i took it off and it
still reads like this.
welling1977 - 24 Jul 2008 16:45 GMT
Forget the above message-- my text box was too short-- (i must have the dumb today!)
welling1977 - 24 Jul 2008 17:04 GMT
Sorry to say, this isn't working. Adding +12 does not effect the date, only the
hour. So, for instance, It's Jul.24 12:01 p (where I am) and I want the clock
to read (Jul. 25 12:01 a) +12 hours for the time change. Instead, the clock
will read Jul.24 12:01a-- the date isn't effected.