我将任务的截止日期保存为今天8:45。但是,日志显示截止日期为
2013-04-26 01:45:46 +0000
是什么给出的?这是我的代码
#import "DatePickerViewController.h"
#import <Parse/Parse.h>
#import "ListItemObject.h"
@class ListItemObject;
@interface DatePickerViewController ()
@end
@implementation DatePickerViewController
@synthesize dateLabel;
@synthesize pick;
@synthesize listFieldText;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
pick.timeZone = [NSTimeZone localTimeZone];
UIBarButtonItem *saveDate = [[UIBarButtonItem alloc]
initWithTitle:@"Save Date"
style:UIBarButtonItemStyleDone
target:self
action:@selector(saveList:)];
self.navigationItem.rightBarButtonItem = saveDate;
[pick addTarget:self action:@selector(updateDateLabel:) forControlEvents:UIControlEventValueChanged];
}
-(IBAction)saveList:(id)sender {
NSLog(@"%@", pick.date);
[ListItemObject saveListItem:[PFUser currentUser] withName:listFieldText withDate:pick.date];
[self.navigationController popViewControllerAnimated:YES];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
-(IBAction)updateDateLabel:(id)sender {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
dateLabel.text = [formatter stringFromDate:pick.date];
}
我是否需要做与NSDateFormatter
相似的事情?
答案 0 :(得分:4)
显示的日期和时间表示与您保存时间相同的时间,但在GMT中。这是因为NSDate
表示单个时间点,没有关于格式化或调整各个时区的时间的信息。记录pick.date返回的NSDate
将以通用GMT显示时间。
使用NSDateFormatter
,就像在updateDateLabel():
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
dateLabel.text = [formatter stringFromDate:pick.date];
将为您提供根据当地时区调整的时间,该时区应与8:45匹配。
答案 1 :(得分:1)
+0000
表示您在UTC时区看到了日期。 NSLog(@"%@", someDate)
只会从description
返回NSDate
属性。如果您希望将值调整为特定时区,则需要设置timeZone
的{{1}}属性。