Categories

Current time and date in Flash

Template-help.com Team November 15, 2010
Rating: 3.7/5. From 3 votes.
Please wait...
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;
This entry was posted in General tutorials, Working with Flash and tagged flash. Bookmark the permalink.

Submit a ticket

If you are still unable to find a sufficient tutorial regarding your issue please use the following link to submit a request to our technical support team. We'll provide you with our help and assistance within next 24 hours: Submit a ticket