HomeAfter Sale Support Working with Flash Advanced Current time and date in Flash

Current time and date in Flash


Q: How can I make my Flash site display current time and date?

A: You can do it with the help of the Date() class available in Action Script. 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 please, 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_min+minutes+" "+AM_PM+" "+day+" "+is_zero_dat+dat+"."+is_zero_mon+month+"."+year;