字符串格式的日期选择器日期已关闭
我有一个只有月、日和年的 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用小写
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.