我試過下面的代碼片段我得到系統的當前日期和時間而不是選定的時區代碼。獲取所選時區的時間和日期?
NSDate *now = [NSDate date];
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:selectedzone]];
NSDateComponents* timeZoneComps = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
NSDate *selectedDate=[gregorian dateFromComponents:timeZoneComps];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss "];
NSString *strSelectedDate= [formatter stringFromDate:selectedDate];
setZone.text = strSelectedDate;`
任何想法?
這是你想要的嗎? – prashant
非常感謝,非常感謝 –