我怎样才能在ios中获得当地时区?

时间:2014-05-28 08:52:17

标签: ios timezone

我需要根据ios设备的位置设置本地时区。

[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; 

[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GTC+5.30"]]; 

适合吗?

4 个答案:

答案 0 :(得分:11)

// get current date/time
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
[dateFormatter release];
NSLog(@"User's current time in their preference format:%@",currentTime);

您可以使用以下方式获取本地时区的名称:

NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *tzName = [timeZone name];

答案 1 :(得分:0)

我就是这样做的:

    NSString *strDateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS";

    // ---------------------------------------------------------
    // input date is UTC date time, need to convert that time
    // to user's own local device timezone to get correct date
    // ---------------------------------------------------------

    NSDateFormatter *dateFormatterUTC = [[NSDateFormatter alloc] init];
    [dateFormatterUTC setDateFormat:strDateFormat];
    [dateFormatterUTC setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    // get UTC Date first
    NSDate* utcDate = [dateFormatterUTC dateFromString:strDate];

    // get local date from UTC Date
    NSDateFormatter *dateFormatterLocal = [[NSDateFormatter alloc] init];
    [dateFormatterLocal setDateFormat:strDateFormat];
    [dateFormatterLocal setTimeZone:[NSTimeZone systemTimeZone]];

    // get final local date calculated by offsetting UTC Date
    NSDate *localDate = [dateFormatterUTC dateFromString:[dateFormatterLocal stringFromDate:utcDate]];

    NSDate *localNowDate = [dateFormatterUTC dateFromString:[dateFormatterLocal stringFromDate:[NSDate date]]];

答案 2 :(得分:0)

NSString *dateStr = @"2012-07-16 07:33:01";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; 
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSLog(@"date : %@",date);

NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;

NSDate *destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date] autorelease];

NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:@"dd-MMM-yyyy hh:mm"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];  
dateStr = [dateFormatters stringFromDate: destinationDate];
NSLog(@"DateString : %@", dateStr);

- (Courtesy: Yuvaraj.M)

答案 3 :(得分:0)

您可以使用NSTimeZone类的localTimeZone类方法来获取相对时区对象,该对象将自身解码为其找到的任何语言环境的默认时区。

SRC:https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSTimeZone_Class/Reference/Reference.html#//apple_ref/occ/clm/NSTimeZone/localTimeZone

如果您需要纬度和经度:在http://www.iana.org/time-zones获取官方时区数据库。下载tzdata2013g.tar.gz。该档案中有一个名为zone.tab的文件,为每个时区提供一个纬度/经度,例如:

 CH      +4723+00832     Europe/Zurich

+ 4723 + 00832表示纬度= +47º23“,经度= +8º23” (SRC:Get Timezone/country from iPhone