UIDatePicker在我打印时显示错误的日期

时间:2015-04-27 01:50:35

标签: ios objective-c datetime uidatepicker

我创建了一个带有UIDatePicker(时间模式)的小项目,并将其连接到值更改方法。在该方法中,我将一个NSLog与所选时间放在一起。当我更改了UIDatePicker的值时,在我的控制台中出现错误的值打印< / p>

例如: 当我选择上午9:19我的控制台打印2015-04-27 03:49:10 +0000

为什么它显示不同的值?

我的代码

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UIDatePicker *picker;
- (IBAction)pic:(id)sender;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



   //Value changed method 
    - (IBAction)pic:(id)sender {
        NSLog(@"%@",self.picker.date);
    }
    @end

1 个答案:

答案 0 :(得分:4)

使用此功能记录

[self.picker.date descriptionWithLocale:[NSLocale currentLocale]]

由于时区日志不正确,NSDate会在显示日期时自动调整时间偏移。