首页 文章

UIDatePicker提前8小时

提问于
浏览
0

我试图在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 回答

  • 0

    将所选日期转换为您当地的时区

    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];
    
  • 1

    从阅读你的评论,没有什么是错的,它只是显示一个不同于你预期的时区 . NSDate到字符串默认为GMT / UTC时间 .

    使用NSDateFormatter控制输出的时区:Apple Documentation for NSDateFormatter

相关问题