SEARCH
Newsletter
Subscribe to get design tips, latest trends, free stuff and more.
It doesn't look like an e-mail address

hosting

  Tutorials Flash & Swish Flash Tutorials Current Time and Date in Flash

Current Time and Date in Flash

Igor Flash Tutorials Feb 13, 2007

Current Time and Date in FlashYou can make your Flash site display current time and date with the help of the Date() class available in ActionScript. This class has all the necessary methods to retrieve full information about the current time. Before using these methods you need to create a date "object" in the class, this is because the methods are not static and they can only be applied to a single object. To create a Date object use the following syntax:

var current_time = new Date();

After that you can use different methods for this object (getDate(), getDay(), getFullYear(), getHours(), getMilliseconds(), getMonth() and so on). Use Flash Help documentation to learn more about the Date() class and its methods.

For example:

current_time.getMonth();
current_time.getFullYear();

Note: You can retrieve both your operating system time and universal time - UTC (known before as GMT Greenwich mean time). To display UTC time you should use the methods that have "UTC" in their names (getUTCDate(), getUTCDay(), getUTCFullYear(), getUTCHours(), getUTCMilliseconds(), getUTCMonth() and so on)

See also: How to set up the current time and date demo movie

Here's the code used in the demo movie:

var today = new Date();
var minutes = today.getMinutes();
var hours = today.getHours();
var dat = today.getDate();
var month = today.getMonth()+1;
var year = today.getFullYear();
var dayN = today.getDay();
switch (dayN) {
case 0 :
day = "Sunday";
break;
case 1 :
day = "Monday";
break;
case 2 :
day = "Tuesday";
break;
case 3 :
day = "Wednesday";
break;
case 4 :
day = "Thursday";
break;
case 5 :
day = "Friday";
break;
case 6 :
day = "Saturday";
break;
}
if (hours>12) {
c_time = (hours-12);
AM_PM = "PM";
}
if (hours==12){
c_time = 12;
AM_PM = "PM";
}
if (hours<12){
c_time = hours;
AM_PM = "AM";
}
if (hours==0){
c_time = 12;
AM_PM = "AM";
}
if (minutes<10) {
is_zero_min = "0";
} else {
is_zero_min = "";
}
if (dat<10) {
is_zero_dat = "0";
} else {
is_zero_dat = "";
}
if (month<10) {
is_zero_mon = "0";
} else {
is_zero_mon = "";
}
output = c_time+":"+is_zero_mi

ABOUT THE AUTHOR

Igor Lognikov

Igor Lognikov is an editor at Web Design Library, the author of many pamphlets related to the web design industry and a fully accredited card-carrying journalist since 1999.

subscribe to newsletter