AS Reference  :  Notes Index  :  Resources  :  About/Contact  :  Downloads

Date Class

An instance of the date class can be used to display the current time, to calculate time between specified date/time combinations (as in displaying countdowns), to show what day of the week a particular date falls on, to generate a random number, or to display a number in UTC format as a date, among other things. To do any of these, an instance of the Date class must be created using the Date constructor:
// 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.

Display the current day/time (Flash 5)

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();
}

Display the current day/time (Flash MX)

With Flash MX, you don't need the blank movieclip to hold the code. Instead it can be put in an onEnterFrame event handler attached to the main timeline (or another movieclip if you prefer). Instead of variable names, the textfields within the movie are given instance names (day_txt, hours_txt, minutes_txt, and seconds_txt) and this is the code (in frame 1 of the main timeline) that fills them in with the current day/time:
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;
};

Display the day of the week for a given date (Flash MX)

To display the day of the week for a particular (numeric) input date, where the input fields are year_txt, month_txt, day_txt, the button movieclip to display the day of week is showdow, and the output field is dayOfWeek_txt, use the getDay date method with an array of day names (notice that we subtract one from the month to allow the user to enter a month 1-12 and have it converted to the 0-11 that actionscript uses):
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()];
};

Use the date object to avoid caching problems

To load an updated version of a swf (eg, into a level) and keep the browser from using a cached copy, the Date object's getTime method provides a random number (number of milliseconds since 1970) which can be appended as a query string (which the browser then treats as a new url).
  url='http://yourdomain/newmovie.swf?basetime='+(new Date()).getTime()

Find a difference between two dates

To find the number of days til next New Years Eve:
  // 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);
 
Intro
Flash: What & How
Example Sites
Create
Draw, Edit Shapes
Gradients
More Drawing Tips
Import
A Sample
Animate
Frames, Keyframes
Motion Tweens
More Motion Tweens
Shape Tweens
Masks
Control
Stop/Replay
Movieclips Intro
Movieclip Reference
Site Structure 1
Slideshow Movieclip
Contact Form
Scroll Resume
Preloader
Site Structure 2
Publish
Display Options
Player Detection
Optimize
AS 2.0 Basics
Intro to Syntax
Playhead Commands
Playhead Cmds 2
Coded Tween
onEnterFrame
Intro to Classes
Declare/Assign
Comments, Trace
Simple Data Types
Arrays & Objects
Code Blocks
Operators
Beyond Buttons
Code Structure
Toggle Controls
Group of Buttons
Drag and Hit
Distort Magnifier
Scroll Text
Bee Game
Dart Shooter
Sound Control
Easing Slider
Easing Slider 2
Components Intro
Timers & Delays
Dynamic Content
Intro
Drawing API
Create Text
Attach Movieclips
Easing Slider 3
Easing Slider 4
Load jpg/swf
Sliding Viewer
Preload swf
XML
Easing Slider 5
Server Comm
LoadVars (w/ PHP)
AS - PHP Lookup
Text File
Database 1:LoadVars
Database 2:Remoting
Read from directory
AS 2.0 Classes
Intro
Math
Key
Date
Color
EventDispatcher
New Samples
Pie Chart
Event-model Emailer
Tween Sequence
Fuse Sequence
SVG in Flash
Bitmap Topo
SWF as Data Holder
Two-level Menu
Yahoo! Flash Maps
Class-based Game
ASTB Samples
Disclaimer
3D Outlines
Bounce Collide
Address Book
Save Drawings
Home  :  Notes Index  :  Resources  :  About/Contact  :  Downloads