Time in ActionScript

ActionScript provides support for working with dates and times through the Date class. Here are some examples of how to use it:

package {
    import flash.display.Sprite;
    import flash.utils.getTimer;

    public class TimeExample extends Sprite {
        public function TimeExample() {
            var p:Function = trace;

            // We'll start by getting the current time.
            var now:Date = new Date();
            p(now);

            // You can create a Date object by providing the year, month, day, etc.
            // Note: Months are 0-indexed in ActionScript (0-11).
            var then:Date = new Date(2009, 10, 17, 20, 34, 58, 651);
            p(then);

            // You can extract the various components of the date value as expected.
            p(then.fullYear);
            p(then.month);
            p(then.date);
            p(then.hours);
            p(then.minutes);
            p(then.seconds);
            p(then.milliseconds);
            p(then.timezoneOffset);

            // The day of the week is also available (0-6, where 0 is Sunday).
            p(then.day);

            // These methods compare two dates, testing if the first occurs before,
            // after, or at the same time as the second, respectively.
            p(then.time < now.time);
            p(then.time > now.time);
            p(then.time == now.time);

            // To get the difference between two dates, you can subtract their time values.
            var diff:Number = now.time - then.time;
            p(diff);

            // We can compute the length of the duration in various units.
            p(diff / (1000 * 60 * 60)); // Hours
            p(diff / (1000 * 60)); // Minutes
            p(diff / 1000); // Seconds
            p(diff); // Milliseconds

            // You can add or subtract time from a Date object.
            p(new Date(then.time + diff));
            p(new Date(then.time - diff));
        }
    }
}

To run this ActionScript code, you would typically compile it into a SWF file and run it in a Flash Player or AIR runtime environment. The output would be similar to the following:

Wed Jul 12 15:30:45 GMT-0700 2023
Tue Nov 17 20:34:58 GMT-0800 2009
2009
10
17
20
34
58
651
480
2
true
false
false
429868547651
119408.0
7164480.0
429868547.0
429868547651.0
Wed Jul 12 15:30:45 GMT-0700 2023
Sat Mar 21 01:34:58 GMT-0800 1996

Note that ActionScript uses the Date class for working with dates and times, which is similar to JavaScript’s Date object. It doesn’t have as many built-in methods for date and time manipulation as Go’s time package, so some operations require manual calculation.

Also, ActionScript doesn’t have a built-in duration type like Go’s Duration. Time differences are represented as numbers of milliseconds.

Lastly, ActionScript doesn’t have a concept of time zones built into the Date class. All dates are created in the local time zone, and you can only get the offset from UTC.

查看推荐产品

Comments powered by Disqus