Flash / Flex / ActionScript/Development/Date

Материал из Web эксперт
Версия от 08:15, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Содержание

Call getter method of Date object

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dOriginal:Date = new Date();
        var nYear:Number = dOriginal.getFullYear();
        var nMonth:Number = dOriginal.getMonth();
        var nDate:Number = dOriginal.getDate();
        var nHour:Number = dOriginal.getHours();
        var nMinute:Number = dOriginal.getMinutes();
        var nSecond:Number = dOriginal.getSeconds();
        var nMillisecond:Number = dOriginal.getMilliseconds();
        var dCopy:Date = new Date(nYear, nMonth, nDate, nHour, nMinute,nSecond, nMillisecond);
        
        trace(dCopy);
    }
  }
}



Create a Date object is by passing it a string representing a time.

 
  
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        new Date("1/28/2007"); // United States style MM/DD/YYYY
        new Date("Sun Jan 28 2007");
        new Date("28 Jan 2007");
        new Date("Sun Jan 28 2007 3:16:12 PM");
    }
  }
}



Creating a Date Object

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dNow:Date = new Date();
        trace(dNow.toString());
    }
  }
}



Get date and month from a Date object

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var now = new Date(  );        
        
        var day = now.getDate(  );     
        var month = now.getMonth(  );  // Returns an integer between 0 and 11
        
        trace(day);
        trace(month);
    }
  }
}



getDay() or getUTCDay() methods return the day of the week as an integer from 0 to 6.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dWhen:Date = new Date(1978, 9, 13, 20);
        trace(dWhen.getDay());
        trace(dWhen.getUTCDay());
    }
  }
}



Get time

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dOriginal:Date = new Date();
        var dCopy:Date = new Date(dOriginal.getTime());

    }
  }
}



Getting the Hours, Minutes, Seconds, and Milliseconds

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dThen:Date = new Date (2727, 9, 27, 5);
        trace(dThen.getHours());  // Displays: 5;
        trace(dThen.getUTCHours());  // Displays: 5 + offset
    }
  }
}



Getting the Time Zone Offset

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dNow:Date = new Date();
        trace("Your time zone offset: " + dNow.getTimezoneOffset() + " minutes");

    }
  }
}



Hours are measured in 24-hour/military time, so they can range from 0 (12 a.m.) to 23 (11 p.m.).

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var timeOfWriting:Date = new Date(2007, 0, 28, 14, 36, 42, 16);
        
        trace(timeOfWriting);

    }
  }
}



Making a Date Based on Epoch Milliseconds: var dDateObject:Date = new Date(epochMilliseconds);

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dSometime:Date = new Date(1150354800000);
        trace(dSometime.toString());
    }
  }
}



Making a Date Based on Year or Month: var dDateObject:Date = new Date(year, month, date, hour, minute, second, millisecond);

 
Parameter      Value Range 
year           0-99, 99 and up, �1801 and down 
month          0 to 11 
date           1 to 31 
hour           0 to 23 
minute         0 to 59 
second         0 to 59 
millisecond    0 to 999 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dSometime:Date = new Date(2006);
        trace(dSometime.toString());
        var dSometime1:Date = new Date(2006, 5);
        trace(dSometime1.toString());
        var dSometime2:Date = new Date(2006, 5, 15);
        trace(dSometime2.toString());
        var dSometime3:Date = new Date(2006, 5, 15, 6);
        trace(dSometime3.toString());
        var dSometime4:Date = new Date(2006, 5, 15, 6, 55);
        trace(dSometime4.toString());
        var dSometime5:Date = new Date(2006, 5, 15, 6, 55, 33);
        trace(dSometime5.toString());
        var dSometime6:Date = new Date(2006, 5, 15, 6, 55, 33, 24);
        trace(dSometime6.toString());
        
    }
  }
}



Pass negative values to these methods. Date starts with 0, January, and subtracts. Thus, a value of �1 is the same as saying December of the previous year.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dSometime:Date = new Date(2003, 5, 23); // June 23, 2003
        dSometime.setMonth(-1);  // December 23, 2002
        trace(dSometime);
    }
  }
}



Send the Date constructor a series of values for the year, month, day of the month, hour, minute, second, and millisecond to represent.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var bday:Date = new Date(1981, 4, 12); // May 12, 1981 12:00:00AM
        
        trace(bday);
    }
  }
}



setFullYear() method takes an integer parameter that is interpreted literally in all ranges, even 0 to 99.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dSometime:Date = new Date();
        dSometime.setFullYear(5);  // Set the year to 5
    }
  }
}



Setting the Date: use the setDate() and setUTCDate() methods to set the day of the month for a Date object.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dSometime:Date = new Date(2003, 5, 23); // June 23, 2003
        
        dSometime.setDate(23);  
        var dSometime:Date = new Date(3433, 1, 8);  
        dSometime.setDate(29);  
        var dSometime:Date = new Date(3433, 1, 8);
        dSometime.setDate(-1);
        dSometime.setMonth(-1);
        
    }
  }
}



Setting the Hours, Minutes, Seconds, and Milliseconds

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dSometime:Date = new Date(2020, 0, 1);
        dSometime.setHours(5);
        dSometime.setMinutes(30);
        dSometime.setSeconds(5);
        dSometime.setMilliseconds(900);
        trace(dSometime.toString());
        dSometime.setMilliseconds(1000);
        
        dSometime.setSeconds(5);  // Set the seconds value back to 5
        dSometime.setMilliseconds(-1);  // 05:30:04:999
    }
  }
}



Setting the Month

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dSometime:Date = new Date();
        dSometime.setMonth(0);  // Set the month to 0, January
        
        var dSometime:Date = new Date(2003, 5, 23);  // June 23, 2003
        dSometime.setMonth(12);  // January 23, 2004
        
    }
  }
}



Setting the Time

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dOriginal:Date = new Date();
        var dCopy:Date = new Date(dOriginal.getTime());
        
        var dOriginal:Date = new Date();
        var dCopy:Date = new Date();
        dCopy.setTime();
    }
  }
}



The toString() method is inherited from the Object class, and it returns a string representing the value.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(new Date());
    }
  }
}



Use an array of the proper names to translate directly

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var timeOfWriting:Date = new Date(2007, 0, 28, 14, 36, 42, 16);
        var daysOfWeek:Array = ["Sunday", "Monday", "Tuesday",
                                "Wednesday", "Thursday",
                                "Friday", "Saturday"];
        trace(daysOfWeek[timeOfWriting.day]);
    }
  }
}



Use four digit to construct the year field

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dWhen:Date = new Date(1978, 9, 13);
        trace(dWhen.getFullYear());  // Displays: 1978
        
        var dThen:Date = new Date(1779, 6, 4);
        trace(dThen.getFullYear());  // Displays: 1779
        
    }
  }
}



Use getUTCFullYear() to determine the UTC year that corresponds to a given local date

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dThen:Date = new Date(1964, 11, 31, 20, 0, 0, 0);
        trace(dThen.getUTCFullYear());
    }
  }
}



Use the getDate() and getUTCDate() methods to return the values of the local day of the month and the corresponding day of the month UTC, respectively.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dThen:Date = new Date(1523, 3, 13);
        trace(dThen.getDate());  // Displays: 13
    }
  }
}



Use UTC method to construct date object

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dLocal:Date = new Date();
        dLocal.setUTCFullYear(1970);
        dLocal.setUTCMonth(0);
        dLocal.setUTCDate(1);
        dLocal.setUTCHours(0);
        dLocal.setUTCMinutes(0);
        dLocal.setUTCSeconds(0);
        dLocal.setUTCMilliseconds(0);
        
        //using the UTC() method, you could write the same thing in this way:
        
        var nUTCDate:Number = Date.UTC(1970, 0, 1);
        var dLocal:Date = new Date(nUTCDate);
    }
  }
}



using the Date class"s static parse() method.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
         trace(Date.parse("1/1/1970")); // 28800000
    }
  }
}



UTC() is a method that returns the time value in milliseconds from midnight on January 1, 1970

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
       // The UTC() method takes the same arguments as the Date constructor: year, month, date, hour, minutes, seconds, milliseconds. 
        var nUTCDate:Number = Date.UTC(1970, 0, 1, 0, 0, 0);
        
        var nUTCDate:Number = Date.UTC(1970, 0, 1);
    }
  }
}