How to get date and time of selected time zone from uipicker in Xcode

前端 未结 2 1836
栀梦
栀梦 2021-01-07 10:20

I\'m trying to display date and time of selected time zone from uipicker in a label or text field I\'m not getting any way to solve this problem, so please any one help me T

2条回答
  •  心在旅途
    2021-01-07 10:50

    To get date and time of selected time zone from uipicker provide date to formatter like this:

    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];
    zone.text = strSelectedDate;
    

提交回复
热议问题