DatePicker 没有返回正确的日期.是不是因为 NSDate 默认为绝对时间? [英] DatePicker is not returning the correct date. Is it because NSDate defaults to absolute time?

查看:55
本文介绍了DatePicker 没有返回正确的日期.是不是因为 NSDate 默认为绝对时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将任务的截止日期保存为今天的 8:45.但是日志显示截止日期为

I saved a task's due date as 8:45 today. However the log shows the due date as

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 所做的事情?

Do I need to do something similar as what I did with NSDateFormatter?

推荐答案

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

The date and time displayed represents the same time as the time you saved, but in GMT. This is because an NSDate represents a single point in time, with no information on formatting or adjusting that time for various time zones. Logging the NSDate returned by pick.date will display the time in generic GMT.

使用 NSDateFormatter,就像您在 updateDateLabel() 中所做的那样:

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

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

will provide you with the time adjusted for your local time zone, which should then match 8:45.

这篇关于DatePicker 没有返回正确的日期.是不是因为 NSDate 默认为绝对时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆