April 2nd, 2005

Using the Date Object

Sometimes it is necessary to display information within Flash on certain days only, until a certain or only after a certain date has passed. This is a simple ActionScript test to set up todays date in the following format "YearMonthDate". Example. 20050402 for today.

Actionscript:

  1. var testDate1 = "20050306"// March 6th, 2005
  2. var testDate2 = "20050406"// April 6th, 2005
  3.  
  4. // create Date object
  5. var now:Date = new Date();
  6.  
  7. // call Month, Day, and FullYear methods
  8. var Month = now.getMonth() + 1// Month is zero index, so add 1
  9. var Day = now.getDate();
  10. var Year = now.getFullYear();
  11.  
  12. // Add zero(0) to front of Month or Day if less than 10
  13. Month = (Month<=9) ? "0"+Month : Month +""// Month + "" is to force String
  14. Day = (Day<=9) ? "0"+Day : Day + ""// Day + "" is to force String
  15.  
  16. // combine into full string
  17. var today:String = Year+Month+Day;
  18.  
  19. trace(today);
  20.  
  21. // simple test of testDate1
  22. if (today>= testDate1) {
  23.     trace("Today matches or exceeds the test date =>("+testDate1+").");
  24. } else {
  25.     trace("Today is not quite enough.  test date =>("+testDate1+").");
  26. }
  27.  
  28. // simple test of testDate2
  29. if (today>= testDate2) {
  30.     trace("Today matches or exceeds the test date =>("+testDate2+").");
  31. } else {
  32.     trace("Today is not quite enough.  test date =>("+testDate2+").");
  33. }

Comments are closed.