如何设置日期选择器的GMT,日期选择器显示错误的时间

时间:2012-05-25 06:52:56

标签: iphone uidatepicker

当我设置时间Zome时,区域设置时区仍然是datepicker返回错误的时间。

datepicker.timeZone = [NSTimeZone localTimeZone];

它应该显示为2012-04-20 12:20 但它显示2012-04-20 12:02:42 + 0000

我如何将印度时区设为datepicker?

2 个答案:

答案 0 :(得分:2)

我认为您应该设置dateformatter的Locale,如下所示:

-(void)localDate:(NSDate*)date
{
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd"]; // set your formatter how you want
    NSDate * t = [dateFormatter dateFromString:[date description]];

    //[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; //you can set date format style
    NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"yourLocalidentifier"] autorelease]; 
    //you can reach your local identifier from here: [NSLocale availableLocaleIdentifiers]

    [dateFormatter setLocale:usLocale];
    NSLog(@"%@",[dateFormatter stringFromDate:t]); 
}

答案 1 :(得分:0)

对于datepicker中的印度时区

目标C

Class backGroundClass = NSClassFromString(@"_UIBarBackground");
for (UIView *view in self.tabBar.subviews) {
    if ([view isKindOfClass:backGroundClass]) {
        for (UIView *view2 in view.subviews) {
            if ([view2 isKindOfClass:[UIImageView class]]) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    view2.backgroundColor = [UIColor redColor];
                });
            };
        };
        break;
    }
}
Swift 3中的

NSTimeInterval seconds = 5 * 60 * 60 + 30;
datePicker.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:seconds]; // Like GMT+5:30