stringFromDate 返回错误的年份 [英] stringFromDate returning the wrong year

查看:74
本文介绍了stringFromDate 返回错误的年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#define stdDateFormat @"YYYYMMdd'T'hh:mm:ssZ"
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:stdDateFormat];
NSString *TimeOfSync = [dateFormat stringFromDate:syncDate];
NSLog(@"date format: %@",stdDateFormat);
NSLog(@"syncDate: %@",syncDate);
NSLog(@"TimeOfSync: %@",TimeOfSync);

记录输出:

date format: YYYYMMdd'T'hh:mm:ssZ
syncDate: 2009-01-03 19:00:00 +0000
TimeOfSync: 20080103T11:00:00-0800

有人可以帮忙吗?我唯一能看到的是输入日期字符串中时区之前的额外空间.谢谢.

Can anyone help? The only thing I can see is the extra space just before the timezone in the input date string. Thanks.

推荐答案

使用小写的 yyyy 代替 YYYY.

Use lowercase yyyy for the year instead of YYYY.

大写 Y 表示日期所在的开始的年份(2009 年 1 月 3 日是从 2008 年开始的一周).

Uppercase Y means the year of the start of the week the date is in (Jan 3, 2009 is in a week that starts in 2008).

请参阅Unicode 日期格式模式.

这篇关于stringFromDate 返回错误的年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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