我需要根据ios设备的位置设置本地时区。
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
或
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GTC+5.30"]];
适合吗?
// 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];
我是这样做的:
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]]];
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)
您可以使用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。选项卡,为每个时区提供时间/长度,例如:
CH +4723+00832 Europe/Zurich
+4723+00832表示纬度= +47º23",经度= +8º23"(SRC:从iPhone获取时区/国家)