字符串格式的日期选择器日期已关闭

发布于 2024-11-16 11:01:58 字数 487 浏览 4 评论 0原文

我有一个只有月、日和年的 UIDatePicker。我正在尝试使用以下代码将所选日期输出到 TextField:

-(IBAction) dateChanged:(id)sender
{
    NSDate *date = datePicker.date;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"M/d/YYYY"];
    expirationDateField.text = [df stringFromDate:date];
    [df release];
}

对于大多数日期,这都有效,但由于某种原因,当我选择 2009 年 1 月 1 日或 2009 年 1 月 2 日时,我最终得到“1/1/ 2008”或“1/2/2008”(这并非特定于 2009 年)。我以为是时区问题,但如果是这样的话,就不会显示 1 月 2 日的错误年份。有什么想法吗?

I have a UIDatePicker that only has month, day, and year. I am trying to output this selected date to a TextField using this code:

-(IBAction) dateChanged:(id)sender
{
    NSDate *date = datePicker.date;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"M/d/YYYY"];
    expirationDateField.text = [df stringFromDate:date];
    [df release];
}

For most dates, this works, but for some reason, when I pick January 1, 2009, or January 2, 2009, I end up getting "1/1/2008" or "1/2/2008" (this isnt specific to 2009). I thought it was a time zone problem, but it wouldn't display the wrong year for January 2 if that were the case. Any ideas?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海螺姑娘 2024-11-23 11:01:58

使用小写 yyyy 表示年份。

大写 YYYY 返回日期所在周的开始年份。
请参阅 Unicode 日期格式模式

Use lowercase yyyy for the year.

Uppercase YYYY returns the year of the start of the week that date falls in.
See Unicode Date Format Patterns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文