【发布时间】:2018-04-09 19:41:57
【问题描述】:
如何根据objective c中的TimeZone获取当前时间?
example-> 亚洲/加尔各答的当前时间。
【问题讨论】:
-
3 票?震惊...网上有很多样本...
标签: ios objective-c iphone
如何根据objective c中的TimeZone获取当前时间?
example-> 亚洲/加尔各答的当前时间。
【问题讨论】:
标签: ios objective-c iphone
试试这个..
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm"];
//Create the date assuming the given string is in GMT
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
//Create a date string in the local timezone
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSString *localDateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"date = %@", localDateString);
如果您想要特定的时区日期,请参阅下面的代码..
NSDate *myDate = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateStyle:NSDateFormatterLongStyle];
[dateFormatter1 setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter1 setDateFormat:@"dd/MM/yyy hh:mm a"];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName: @"Asia/Calcutta"];
[dateFormatter1 setTimeZone:timeZone];
NSString *dateString = [dateFormatter1 stringFromDate: myDate];
NSLog(@"%@", dateString);
【讨论】: