码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 系统时间格式区别

时间:2014-12-29 21:16:20      阅读:506      评论:0      收藏:0      [点我收藏+]

标签:

  今天,遇到一个奇怪的问题,以前也没有注意,当然很少这样写,因为公司项目部分是由外包公司开发的,也没有仔细的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.

 

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 系统时间格式区别

标签:

原文地址:http://www.cnblogs.com/simple-life-no1/p/4192311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!