2012-06-14 27 views
0

我試圖在iPhone上構建一個datepicker程序。然而,文字顯示的時間比我選擇的時間早8小時。 這是代碼。UIDatePicker提前8小時

「h」 的文件:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController{ 
    UIDatePicker *datepick; 
    IBOutlet UILabel *label; 
    IBOutlet UITextField *textfield; 
} 
-(IBAction)button; 
@property (nonatomic, retain)IBOutlet UIDatePicker *datepick; 
@end 

「M」 的文件:

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize datepick; 

-(IBAction)button { 
    NSDate *choice = [datepick date]; 
    NSString *words = [[NSString alloc]initWithFormat:@"The date is %@", choice]; 

    UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"the title" message:words 
    delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil]; 
    [alert show]; 
    label.text = words; 
    textfield.text = words; 
} 
+0

?什麼是輸出,你期望什麼?可能你正在尋找'NSDateFormatter'來格式化你想要的日期。 – skram

+0

我選擇日期如6月7日...,但是6月6日...顯示。 – coolhongly

回答

1

轉換選定的日期到本地時區

NSDate *choice = [datepick date]; 

NSDateFormatter *dateFormat = [[NSDateFormatter new] autorelease]; 
[dateFormat setTimeZone:[NSTimeZone localTimeZone]]; 
[dateFormat setDateFormat:@"yyyy-MM-dd h:mma"]; 

NSString *strDate = [dateFormat stringFromDate:choice]; 
0

從閱讀您的意見,什麼是錯的,它只是顯示比你預期的不同的時區。 NSDate默認字符串轉到GMT/UTC時間。

使用一個NSDateFormatter的時區是輸出控制:當你說這不正是Apple Documentation for NSDateFormatter