如何查看我的设备时间在哪个时区?

时间:2018-07-19 07:55:10

标签: ios swift4

我从API获得的时间戳为EST,如果我的设备在印度或美国,则应该根据时区显示(IST或CST)时间。我该怎么办?

[]

如何获得EST,CST,EDT等,而不是获得亚洲/加尔各答?

5 个答案:

答案 0 :(得分:0)

您可以从名称属性中获取名称:

 NSLog(@"Name : %@", timeZone.name);

输出将是:

2015-07-10 13:31:46.292 APP[2904:50429] Name : Asia/Calcutta

然后您可以获取所需样式的名称:

NSTimeZone* timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"];
NSString* timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]];
NSLog(@"Name : %@", timeZoneName);

NSLog(@"%@",[NSTimeZone abbreviationDictionary]);

输出将是:

2015-07-10 11:44:05.954 APP[1472:26120] {
    ADT = "America/Halifax";
    AKDT = "America/Juneau";
    AKST = "America/Juneau";
    ART = "America/Argentina/Buenos_Aires";
    AST = "America/Halifax";
    BDT = "Asia/Dhaka";
    BRST = "America/Sao_Paulo";
    BRT = "America/Sao_Paulo";
    BST = "Europe/London";
    CAT = "Africa/Harare";
    CDT = "America/Chicago";
    CEST = "Europe/Paris";
    CET = "Europe/Paris";
    CLST = "America/Santiago";
    CLT = "America/Santiago";
    COT = "America/Bogota";
    CST = "America/Chicago";
    EAT = "Africa/Addis_Ababa";
    EDT = "America/New_York";
    EEST = "Europe/Istanbul";
    EET = "Europe/Istanbul";
    EST = "America/New_York";
    GMT = GMT;
    GST = "Asia/Dubai";
    HKT = "Asia/Hong_Kong";
    HST = "Pacific/Honolulu";
    ICT = "Asia/Bangkok";
    IRST = "Asia/Tehran";
    IST = "Asia/Calcutta";
    JST = "Asia/Tokyo";
    KST = "Asia/Seoul";
    MDT = "America/Denver";
    MSD = "Europe/Moscow";
    MSK = "Europe/Moscow";
    MST = "America/Denver";
    NZDT = "Pacific/Auckland";
    NZST = "Pacific/Auckland";
    PDT = "America/Los_Angeles";
    PET = "America/Lima";
    PHT = "Asia/Manila";
    PKT = "Asia/Karachi";
    PST = "America/Los_Angeles";
    SGT = "Asia/Singapore";
    UTC = UTC;
    WAT = "Africa/Lagos";
    WEST = "Europe/Lisbon";
    WET = "Europe/Lisbon";
    WIT = "Asia/Jakarta";
}

然后您可以找到当前的时区。

答案 1 :(得分:0)

let timeStr = "15/02/2014 01:00:00"

let currentTimezone = TimeZone.current
let targetTimezone = TimeZone(abbreviation: "EST") ?? TimeZone.current

let dateFormatter1 = DateFormatter()
dateFormatter1.dateFormat = "dd/MM/yyyy HH:mm:ss"

let localOffset = currentTimezone.secondsFromGMT()
let targetOffset = targetTimezone.secondsFromGMT()


let localDate = dateFormatter1.date(from: timeStr)


let targetDate = localDate?.addingTimeInterval(TimeInterval(targetOffset - localOffset))

尝试一下。 TimeZone.current将返回系统当前使用的时区。

答案 2 :(得分:0)

尝试此代码:

let timeZone = TimeZone.current
let name = timeZone.localizedName(for: .shortStandard, locale: Locale.current)
print("Name :: ", name)

答案 3 :(得分:0)

请尝试一下。它对我有用。

请以UTC格式从服务器获取时间。 然后:

func convertFromUTCtoLocal(dateToConvert:String) -> String {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssz"
    let convertedDate = formatter.date(from: dateToConvert)
    formatter.timeZone = TimeZone(abbreviation: "EST")
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    return formatter.string(from: convertedDate!)
}

根据您的要求设置时区,这可能会对您有所帮助。谢谢

答案 4 :(得分:-1)

ObjC:

NSTimeZone *timeZoneLocal = [NSTimeZone localTimeZone];
NSString *strTimeZoneAbb = [timeZoneLocal abbreviation];
NSString *strTimeZoneName = [timeZoneLocal name];
NSLog(@"%@",strTimeZoneAbb);  //Ex:Output:GMT +5:10
NSLog(@"%@",strTimeZoneName);  //Ex:Output:Asia/Calcutta

迅速:

let timezone = NSTimeZone.local
print(timezone.abbreviation)
print(timezone.name)