iOS 系统时间格式区别
2014-12-29 19:58
162 查看
今天,遇到一个奇怪的问题,以前也没有注意,当然很少这样写,因为公司项目部分是由外包公司开发的,也没有仔细的review代码。由于iOS时间格式为“YYYY-MM-dd”引起时间相差一年,也就是每年的12月29日开始后一周里如果这样使用会引起时间相差一年。我查了一下资料,具体事说YYYY代表 “week of year”,yyyy代表calendar year。(细节问题,相信很多人以前都没注意!)
原文:
A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.
使用YYYY 是根据每年按52周或者53周得到年份,它规定每年的12月28一定是本年的。也可以这样理解就是如果1月1号是星期五、星期六、星期天,那么接下来的属于这个星期的这几天都是前一年的,这一周是本年的最后一周。如果1月1号是星期一、星期二、星期三、星期四,这周的时间(前一年可能还有0-3天是本周的哦)都算新的一年的第一周。这样就清楚了!知道原因了也好解决。
下面介绍其他基本的时间格式含义(网上复制的)。
时间分隔符。在某些区域设置中,可以使用其他字符表示时间分隔符。时间分隔符在格式化时间值时分隔小时、分钟和秒。格式化输出中用作时间分隔符的实际字符由您的应用程序的当前区域性值确定。
(/)
日期分隔符。在某些区域设置中,可以使用其他字符表示日期分隔符。日期分隔符在格式化日期值时分隔日、月和年。格式化输出中用作日期分隔符的实际字符由您的应用程序的当前区域性确定。
(%)
用于表明不论尾随什么字母,随后字符都应该以单字母格式读取。也用于表明单字母格式应以用户定义格式读取。有关更多详细信息,请参见下面的内容。
d
将日显示为不带前导零的数字(如 1)。如果这是用户定义的数字格式中的唯一字符,请使用 %d。
dd
将日显示为带前导零的数字(如 01)。
EEE
将日显示为缩写形式(例如 Sun)。
EEEE
将日显示为全名(例如 Sunday)。
M
将月份显示为不带前导零的数字(如一月表示为 1)。如果这是用户定义的数字格式中的唯一字符,请使用 %M。
MM
将月份显示为带前导零的数字(例如 01/12/01)。
MMM
将月份显示为缩写形式(例如 Jan)。
MMMM
将月份显示为完整月份名(例如 January)。
gg
显示时代/纪元字符串(例如 A.D.)
h
使用 12 小时制将小时显示为不带前导零的数字(例如 1:15:15 PM)。如果这是用户定义的数字格式中的唯一字符,请使用 %h。
hh
使用 12 小时制将小时显示为带前导零的数字(例如 01:15:15 PM)。
H
使用 24 小时制将小时显示为不带前导零的数字(例如 1:15:15)。如果这是用户定义的数字格式中的唯一字符,请使用 %H。
HH
使用 24 小时制将小时显示为带前导零的数字(例如 01:15:15)。
m
将分钟显示为不带前导零的数字(例如 12:1:15)。如果这是用户定义的数字格式中的唯一字符,请使用 %m。
mm
将分钟显示为带前导零的数字(例如 12:01:15)。
s
将秒显示为不带前导零的数字(例如 12:15:5)。如果这是用户定义的数字格式中的唯一字符,请使用 %s。
ss
将秒显示为带前导零的数字(例如 12:15:05)。
f
显示秒的小数部分。例如,ff 将精确显示到百分之一秒,而 ffff 将精确显示到万分之一秒。用户定义格式中最多可使用七个 f 符号。如果这是用户定义的数字格式中的唯一字符,请使用 %f。
t
使用 12 小时制,并对中午之前的任一小时显示大写的 A,对中午到 11:59 P.M 之间的任一小时显示大写的 P。如果这是用户定义的数字格式中的唯一字符,请使用 %t。
tt
对于使用 12 小时制的区域设置,对中午之前任一小时显示大写的 AM,对中午到 11:59 P.M 之间的任一小时显示大写的 PM。
对于使用 24 小时制的区域设置,不显示任何字符。
y
将年份 (0-9) 显示为不带前导零的数字。如果这是用户定义的数字格式中的唯一字符,请使用 %y。
yy
以带前导零的两位数字格式显示年份(如果适用)。
yyy
以四位数字格式显示年份。
yyyy
以四位数字格式显示年份。
z
显示不带前导零的时区偏移量(如 -8)。如果这是用户定义的数字格式中的唯一字符,请使用 %z。
zz
显示带前导零的时区偏移量(例如 -08)
zzz
显示完整的时区偏移量(例如 -08:00)
原文:
A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.
Both results are correct, the difference is because the DateUtils.WeekOfTheYear function uses the ISO 8601 standard definition of a week. That is, a week is considered to start on a Monday and end on a Sunday. Check this explanation about the Weeks Numbers in the ISO 8601 format. If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year (there is no week 00). 28 December is always in the last week of its year. Also the Embarcadero online help for the DateUtils.WeekOfTheYear function says ....if the first calendar day of the year is a Friday, Saturday, or Sunday, then for the first three, two, or one days of the calendar year, WeekOfTheYear returns the last week of the previous year. Similarly, if the last calendar day of the year is a Monday, Tuesday, or Wednesday, then for the last one, two, or three days of the calendar year, WeekOfTheYear returns 1 (the first week of the next calendar year). So due which the 01-01-2011 was Saturday this week is considered the week number 52 of the previous year. And that also explains why the week number returned for today (19-10-2011) is 42 instead of 43.
使用YYYY 是根据每年按52周或者53周得到年份,它规定每年的12月28一定是本年的。也可以这样理解就是如果1月1号是星期五、星期六、星期天,那么接下来的属于这个星期的这几天都是前一年的,这一周是本年的最后一周。如果1月1号是星期一、星期二、星期三、星期四,这周的时间(前一年可能还有0-3天是本周的哦)都算新的一年的第一周。这样就清楚了!知道原因了也好解决。
下面介绍其他基本的时间格式含义(网上复制的)。
时间分隔符。在某些区域设置中,可以使用其他字符表示时间分隔符。时间分隔符在格式化时间值时分隔小时、分钟和秒。格式化输出中用作时间分隔符的实际字符由您的应用程序的当前区域性值确定。
(/)
日期分隔符。在某些区域设置中,可以使用其他字符表示日期分隔符。日期分隔符在格式化日期值时分隔日、月和年。格式化输出中用作日期分隔符的实际字符由您的应用程序的当前区域性确定。
(%)
用于表明不论尾随什么字母,随后字符都应该以单字母格式读取。也用于表明单字母格式应以用户定义格式读取。有关更多详细信息,请参见下面的内容。
d
将日显示为不带前导零的数字(如 1)。如果这是用户定义的数字格式中的唯一字符,请使用 %d。
dd
将日显示为带前导零的数字(如 01)。
EEE
将日显示为缩写形式(例如 Sun)。
EEEE
将日显示为全名(例如 Sunday)。
M
将月份显示为不带前导零的数字(如一月表示为 1)。如果这是用户定义的数字格式中的唯一字符,请使用 %M。
MM
将月份显示为带前导零的数字(例如 01/12/01)。
MMM
将月份显示为缩写形式(例如 Jan)。
MMMM
将月份显示为完整月份名(例如 January)。
gg
显示时代/纪元字符串(例如 A.D.)
h
使用 12 小时制将小时显示为不带前导零的数字(例如 1:15:15 PM)。如果这是用户定义的数字格式中的唯一字符,请使用 %h。
hh
使用 12 小时制将小时显示为带前导零的数字(例如 01:15:15 PM)。
H
使用 24 小时制将小时显示为不带前导零的数字(例如 1:15:15)。如果这是用户定义的数字格式中的唯一字符,请使用 %H。
HH
使用 24 小时制将小时显示为带前导零的数字(例如 01:15:15)。
m
将分钟显示为不带前导零的数字(例如 12:1:15)。如果这是用户定义的数字格式中的唯一字符,请使用 %m。
mm
将分钟显示为带前导零的数字(例如 12:01:15)。
s
将秒显示为不带前导零的数字(例如 12:15:5)。如果这是用户定义的数字格式中的唯一字符,请使用 %s。
ss
将秒显示为带前导零的数字(例如 12:15:05)。
f
显示秒的小数部分。例如,ff 将精确显示到百分之一秒,而 ffff 将精确显示到万分之一秒。用户定义格式中最多可使用七个 f 符号。如果这是用户定义的数字格式中的唯一字符,请使用 %f。
t
使用 12 小时制,并对中午之前的任一小时显示大写的 A,对中午到 11:59 P.M 之间的任一小时显示大写的 P。如果这是用户定义的数字格式中的唯一字符,请使用 %t。
tt
对于使用 12 小时制的区域设置,对中午之前任一小时显示大写的 AM,对中午到 11:59 P.M 之间的任一小时显示大写的 PM。
对于使用 24 小时制的区域设置,不显示任何字符。
y
将年份 (0-9) 显示为不带前导零的数字。如果这是用户定义的数字格式中的唯一字符,请使用 %y。
yy
以带前导零的两位数字格式显示年份(如果适用)。
yyy
以四位数字格式显示年份。
yyyy
以四位数字格式显示年份。
z
显示不带前导零的时区偏移量(如 -8)。如果这是用户定义的数字格式中的唯一字符,请使用 %z。
zz
显示带前导零的时区偏移量(例如 -08)
zzz
显示完整的时区偏移量(例如 -08:00)
相关文章推荐
- ios 在中国地区,24小时时间格式 系统设定下 获得12小时制时间的方法
- 年月日时间格式转时间戳 ,ios系统不兼容 JS 中 = new Date(yyyy-mm-dd)方法
- iOS 获取系统时间以及时间与日期格式互相转换
- 在java中取得当前的系统时间并且转化成自己想要的格式
- Java系统时间 格式
- j2me中获取系统时间并转换成字符串格式
- j2me中如何使用Calendar获取系统时间并转换成字符串格式
- 系统文件格式FAT32与NTFS的区别
- 获取系统时间及格式
- 系统日期时间格式设置
- j2me中获取系统时间并转换成字符串格式
- j2me获取系统时间(YYMMDDhhmm)格式
- 关于系统时间格式的问题
- DateTimeToStr()与FormatDateTime()的区别,时间格式不对引起的错误解决
- Java 获取当前系统时间 格式:yyyy-MM-dd HH:mm:ss
- 利用javascript得到系统当前时间(含有3种格式)
- 以字符串形式返回当前系统时间,默认格式为“年月日时分秒”
- Windows 驱动:获取系统当前时间,生成格式字符串
- 调试“CAD电子文件收集系统”中的bug(关于应用程序域和时间格式)
- 如何修改win2003服务器系统时间格式