如果您创建NSDate 实例,则默认情况下它将以UTC or GMT 格式返回日期。
现在,当您通过任何日期格式化程序将此日期转换为字符串时,返回的字符串(日期)将采用本地时区(即设备的时区)。
您可以使用其名称创建自定义时区以获取该时区的日期。
NSDate *date = [NSDate date]; //give current date in UTC or GMT
NSLog(@"current date in gmt : %@",date);
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateInStrFormat = [df stringFromDate:date]; // this date(string format) will be in current timezone that was set in your device and
NSLog(@"current date in local or device's default timezone : %@",dateInStrFormat);
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
NSLog(@"timezone : %@",timeZone);
[df setTimeZone:timeZone];
NSString *dateInCustomTimeZone = [df stringFromDate:date]; // this will return date in Asia/Kolkata's timezone
NSLog(@"date in custom timezone : %@",dateInCustomTimeZone);
NSLog(@"timezone : %@",timeZone); 会打印出类似Asia/Kolkata (IST) offset 19800 的内容。 19800 是偏移量,如果你将它与3600 分开,那么你会得到与gmt 的差异,比如(+/- 5.30 etc)
Link for different timezone names
或者你可以得到时区,
NSTimeZone *timezone1 = [NSTimeZone timeZoneWithName:@"GMT+5:30"];
NSLog(@"timezone : %@",timezone1);
NSTimeZone *timeAone2 = [NSTimeZone timeZoneForSecondsFromGMT:60*60*5.5];
NSLog(@"timezone : %@",timeAone2);