字符串格式的日期选择器日期已关闭 [英] String formatted date picker date is off

查看:148
本文介绍了字符串格式的日期选择器日期已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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];
}

对于大多数日期,这都有效,但出于某种原因,当我选择1月份时2009年1月1日,或2009年1月2日,我最终获得1/1/2008或1/2/2008(这不是2009年特定的)。我认为这是一个时区问题,但如果是这样的话,它将不会显示1月2日的错误年份。任何想法?

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?

推荐答案

年份使用小写 yyyy

大写 YYYY 返回该日期所在星期的开始年份。

请参阅 Unicode日期格式模式

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

这篇关于字符串格式的日期选择器日期已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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