// create variable dateNow with the current date var dateNow = new Date(); // create variable with specified date var dateBirthday = new Date(1987,2,22); // create variable with date from number var dateFromNumber = new Date(543387600000); // create variable with specified date+time var dateBirthTime = new Date(1987,2,22,1,32);
So with no parameters specified (first example), the Date constructor returns the current date and time on the user's machine. If "year,month,day" is specified, Date returns that specified date with a time of 0:00. For "year,month,day,hour,minute", Date gives you that exact date and time. And for a number, Date will convert it to the equivalent date (in the number example above, we found the number by doing a trace("Your birthday=" + dateBirthday), which returned the date as a numeric string).
Once you have created a date variable from the Date constructor, you can then display that date, extract whatever part of it you want with various date methods like getMonth or getHours, or do further calculations with it.
To create a continuous display of the time on a viewer's machine, you must continually recreate instances of the Date class. To do this in Flash 5, we used a blank controller clip with an onClipEvent(load) routine to initialize things and an onClipEvent(enterFrame) routine to update them.
We create a new date instance and an array of day names in the load routine. In the enterFrame routine, we look at each variable to be displayed (hours, minutes, seconds) and format them correctly. After this the date instance is destroyed and a new one created. Here are the routines:
onClipEvent(load){
dateNow = new Date();
aDays= new Array('Sunday', 'Monday', ... 'Saturday');
}
onClipEvent(enterFrame){
_parent.sDay = aDays[dateNow.getDay()];
_parent.nHour = dateNow.getHours();
_parent.nMinutes = dateNow.getMinutes();
if (_parent.nMinutes < 10){
_parent.nMinutes ="0"+_root.nMinutes;
}
parent.nSeconds = int(dateNow.getSeconds());
if (parent.nSeconds < 10){
parent.nSeconds = "0"+_root.nSeconds;
delete dateNow;
dateNow = new Date();
}
var days:Array = ['Sunday', 'Monday', ...'Saturday'];
this.onEnterFrame = function() {
timenow = new Date();
day_txt.text = days[timenow.getDay()];
hours_txt.text = timenow.getHours();
var nMinutes:Number = timenow.getMinutes();
minutes_txt.text = nMinutes < 10 ? "0" + nMinutes : nMinutes;
var nSeconds:Number = int(timenow.getSeconds());
seconds_txt.text = nSeconds < 10 ? "0" + nSeconds : nSeconds;
};
showdow.onRelease = function() {
var days = new Array('Sunday', 'Monday', 'Tuesday',
'Wednesday','Thursday','Friday', 'Saturday');
var this_date = new Date(Number(year_txt.text), Number(month_txt.text)-1,
Number(day_txt.text));
dayOfWeek_txt.text = days[this_date.getDay()];
};
url='http://yourdomain/newmovie.swf?basetime='+(new Date()).getTime()
// get current year, make date using that with December (month=11), day 31 var nyeve_date = new Date((new Date()).getFullYear(), 11, 31); var now_date = new Date(); // convert difference in milliseconds to days var nDiffDays = Math.floor((nyeve_date - now_date)/86400000); trace(nDiffDays);
Discussed on this page:
get current date or time, set date variable, date methods, clock, append date string to keep from pulling cached version of swf