如果您創建了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);
你需要GMT + 5:30或GMT +5 –