我试图在iPhone上 Build 一个datepicker程序 . 但是,文字中显示的时间比我选择的时间早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;
}
2 回答
将所选日期转换为您当地的时区
从阅读你的评论,没有什么是错的,它只是显示一个不同于你预期的时区 . NSDate到字符串默认为GMT / UTC时间 .
使用NSDateFormatter控制输出的时区:Apple Documentation for NSDateFormatter