NSD 推迟一天

发布于 2024-12-22 13:48:07 字数 1787 浏览 1 评论 0原文

在我目前正在开发的 iPhone 应用程序中,我遇到了一些有关日期的问题。我没有广泛使用它们,所以这是非常基本的,但不知何故我仍然做错了。当我通过简单的月/日/年 UIDatePicker 获取 NSDate 时,我会选择例如 2011 年 12 月 24 日。但是,当我通过 NSDateFormatter 根据本地时区打印 NSDate 时,我将得到比我选择的日期少一天的 MM-DD-YY 输出。这是我所有相关的日期输入/输出代码:

我正在通过 UIDatePicker 使用以下方法获取与自定义对象关联的日期:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet // Protocol method for UIActionSheetDelegate
{

// Among other things, add our UIDatePicker to a UIActionSheet that we pop up.

NSDate* currentDate = [NSDate date];

[pickerView setDate:currentDate];

[pickerView setMaximumDate:currentDate];

[pickerView setDatePickerMode:UIDatePickerModeDate]; // We don't need time for this.

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ // UIActionSheetDelegate Protocol method for saving date change if OK is selected, else if cancel is selected, do nothing.

    [self.customObjectBeingInput setDate:[ourDatePicker date]]; // Set object date to selected date in date picker.
}

至于将日期输出到我设置的 UILabel,这是发生的事情那里:

NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; // Format the date into human-readble format.
    [dateformatter setDateFormat:@"MM-dd-yyyy"];
    [dateformatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateformatter setDateStyle:NSDateFormatterShortStyle];
    NSString *formattedDate = [dateformatter stringFromDate:[self.customObject date]];
    [[self.customViewController customObjectDateLabel] setText:formattedDate];
    [dateformatter release];
    dateformatter = nil;

我是否应该为 UIDatePicker 输入设置与时区相关的内容,或者由于 NSDate 返回绝对时间点,而不管用户的时区如何,我们一般可以至少在输入阶段忽略局部性问题?

我绝对认为这与输出阶段有关,而不是与我如何通过 UIDatePicker 获取日期有关,但我不知道自己做错了什么根本不做。

in an iPhone app I'm currently developing, I'm having some issues with regard to dates. I don't make extensive use of them, so this is pretty basic, but I'm somehow still doing it wrong. When I take my NSDate in through a simple Month/Day/Year UIDatePicker, I'll select say December 24, 2011 for example. However, when I print out the NSDate according to the local time zone via an NSDateFormatter, I'll get a MM-DD-YY output that is a day less than the day I selected. Here is all of my relevant date input/output code:

I'm taking in a date associated with a custom object via a UIDatePicker with the following methods:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet // Protocol method for UIActionSheetDelegate
{

// Among other things, add our UIDatePicker to a UIActionSheet that we pop up.

NSDate* currentDate = [NSDate date];

[pickerView setDate:currentDate];

[pickerView setMaximumDate:currentDate];

[pickerView setDatePickerMode:UIDatePickerModeDate]; // We don't need time for this.

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ // UIActionSheetDelegate Protocol method for saving date change if OK is selected, else if cancel is selected, do nothing.

    [self.customObjectBeingInput setDate:[ourDatePicker date]]; // Set object date to selected date in date picker.
}

As far as outputting the date to a UILabel I have setup, here is what's going on there:

NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; // Format the date into human-readble format.
    [dateformatter setDateFormat:@"MM-dd-yyyy"];
    [dateformatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateformatter setDateStyle:NSDateFormatterShortStyle];
    NSString *formattedDate = [dateformatter stringFromDate:[self.customObject date]];
    [[self.customViewController customObjectDateLabel] setText:formattedDate];
    [dateformatter release];
    dateformatter = nil;

Am I supposed to be setting something related to time zones for the UIDatePicker input, or since an NSDate returns an absolute point in time regardless of the user's time zone, we can generally ignore locality issues, at least in the input stage?

I definitely think this has to do with the output stage and not with how I'm taking in the date through the UIDatePicker, but I'm at a loss as to what I'm doing wrong or not doing at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文