-1

我将任务的截止日期保存为今天 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

4

2 回答 2

4

显示的日期和时间与您保存的时间相同,但以 GMT 表示。这是因为 anNSDate表示单个时间点,没有关于为不同时区格式化或调整该时间的信息。记录NSDatepick.date 返回的日志将以通用 GMT 显示时间。

使用NSDateFormatter, 就像你在updateDateLabel():

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
dateLabel.text = [formatter stringFromDate:pick.date];

将为您提供根据您当地时区调整的时间,该时间应与 8:45 匹配。

于 2013-04-26T02:15:24.503 回答
1

+0000意味着您看到的是 UTC 时区的日期。NSLog(@"%@", someDate)只会descriptionNSDate. 如果您希望将值调整为特定时区,则需要设置timeZone.NSDateFormatter

于 2013-04-26T02:12:23.970 回答