如何使用Flex / AS3获取浏览器时区?

时间:2011-03-04 01:32:37

标签: flex actionscript-3 timezone

如何在Flex / AS3中获取客户端浏览器的时区?

3 个答案:

答案 0 :(得分:1)

您可以使用Date对象的timeZoneOffest吗?

老实说,我相信这只会根据用户的操作系统设置传回信息。我不希望bBrowser实际上有一个时区。

答案 1 :(得分:1)

我一直想要同样的事情,但我在网上找不到任何东西。我已经在使用FlexDateUtilsblog post)了,但它只将时区格式化为'GMT -600'。幸运的是,我可以肯定地说我的应用程序的用户将在美国,所以我修改为'DateUtils.buildTimeZoneDesignation(date:Date)',如下所示。我希望这会有所帮助。

    private static function buildTimeZoneDesignation( date:Date ):String {
        if ( !date ) {
            return "";
        }
        var timezoneOffsetHours:Number = date.getTimezoneOffset() / 60;

        // custom timezone handlers (assumes major U.S. zones with daylight savings time dates as of 2011)
        if (3 < timezoneOffsetHours && timezoneOffsetHours < 12)
        {
            var usDST:Boolean = false;
            // the date of the Sunday before today unless today is Sunday
            var sundayBeforeToday:Number = date.date - date.day;
            if (2007 <= date.fullYear) {
                // test for since-2007 extended daylight savings time
                if (2 < date.month && date.month < 10) {
                    // daylight savings time (April through October)
                    usDST = true;
                }
                else if (date.month == 2) {
                    // DST starts second Sunday in March
                    usDST = (7 < sundayBeforeToday);
                }
                else if (date.month == 10) {
                    // DST ends first Sunday in November
                    usDST = (0 < sundayBeforeToday);
                }
            }
            else {
                // test for pre-2007 daylight savings time
                if (3 < date.month && date.month < 9) {
                    // daylight savings time (May through September)
                    usDST = true;
                }
                else if (date.month == 3) {
                    // DST starts first Sunday in April
                    usDST = (0 < sundayBeforeToday);
                }
                else if (date.month == 9) {
                    // DST ends last Sunday in October
                    usDST = (sundayBeforeToday + 7 <= 31);
                }
            }
            // return custom timezone strings for US timezones
            switch (timezoneOffsetHours) {
                case 4:
                    // Eastern or Atlantic
                    return usDST ? "EDT" : "AST";
                case 5:
                    // Central or Eastern
                    return usDST ? "CDT" : "EST";
                case 6:
                    // Mountain or Central
                    return usDST ? "MDT" : "CST";
                case 7:
                    // Pacific or Mountain
                    return usDST ? "PDT" : "MST";
                case 8:
                    // Alaska or Pacific
                    return usDST ? "AKDT" : "PST";
                case 9:
                    // Hawaii or Alaska
                    return usDST ? "HADT" : "AKST";
                case 10:
                    // Samoa or Hawaii
                    return usDST ? "SDT" : "HAST";
                case 11:
                    if (!usDST)
                        // Samoa
                        return "SST";
                    break;
            }
        }

        // else just generate a GMT string

        var timeZoneAsString:String = "GMT ";
        // timezoneoffset is the number that needs to be added to the local time to get to GMT, so
        // a positive number would actually be GMT -X hours
        if ( 0 < timezoneOffsetHours && timezoneOffsetHours < 10 ) {
            timeZoneAsString += "-0" + ( timezoneOffsetHours ).toString();
        } else if ( date.getTimezoneOffset() < 0 && timezoneOffsetHours > -10 ) {
            timeZoneAsString += "0" + ( -1 * timezoneOffsetHours ).toString();
        }
        // add zeros to match standard format
        timeZoneAsString += "00";

        return timeZoneAsString;
    }

答案 2 :(得分:1)