第一次寫(xiě)簡(jiǎn)書(shū)有點(diǎn)小激動(dòng)哈,關(guān)于時(shí)間戳這個(gè)我一直很混亂用的時(shí)候就胡亂粘貼完成需求炸枣,現(xiàn)在有時(shí)間了整理下厌殉。NSDate*datenow = [NSDatedate];
NSLog(@"datenow = %@",datenow);
文檔如下
Creates and returns a new date set to the current date and time.
A new date object set to the current date and time.
Returns A new date object set to the current date and time.
大概意思是說(shuō)
創(chuàng)建并返回一個(gè)新的日期設(shè)置為當(dāng)前日期和時(shí)間寸五。
設(shè)置為當(dāng)前日期和時(shí)間旁蔼,一個(gè)新的Date對(duì)象锨苏。
返回設(shè)置為當(dāng)前日期和時(shí)間,一個(gè)新的Date對(duì)象棺聊。
首先初始化NSDate ?并且打印了他結(jié)果是這樣的
結(jié)果他與本地時(shí)間相差 8個(gè)小時(shí)? 原因是好像是從ios4.1開(kāi)始[NSDate date]返回的是GMT伞租,GMT是中央時(shí)區(qū),北京在東8區(qū),相差8個(gè)小時(shí)(原諒我地理知識(shí)全還給老師了)那怎么辦呢,接下來(lái)看
NSTimeZone*zone = [NSTimeZonesystemTimeZone];
NSLog(@"systemTimeZone = %@",zone);
文檔如下
Returns the time zone currently used by the system.
The time zone currently used by the system. If the current time zone cannot be determined, returns the GMT time zone.
If you get the system time zone, it is cached by the application and does not change if the user subsequently changes the system time zone. The next time you invoke systemTimeZone, you get back the same time zone you originally got. You have to invoke resetSystemTimeZone to clear the cached object.
大概意思:
NSTimeZone 返回的是系統(tǒng)設(shè)定好的時(shí)區(qū) 如果無(wú)法確定時(shí)區(qū)那么久返回中央時(shí)區(qū)限佩。而且這個(gè)時(shí)區(qū)是被緩存起來(lái)的葵诈,就算獲取后用戶改變了時(shí)區(qū),系統(tǒng)還是獲取第一次獲取的時(shí)區(qū)犀暑,否則你必須調(diào)用 調(diào)用resetSystemTimeZone清除緩存的對(duì)象驯击。(文檔是好東西)烁兰。
然后我就實(shí)驗(yàn)下 時(shí)區(qū)設(shè)置成英國(guó)蛋疼的事情發(fā)生了? 我沒(méi)調(diào)用 resetSystemTimeZone 他還是更改了時(shí)區(qū) 這文檔毛意思耐亏!求大神們指正 resetSystemTimeZone 有毛用啊沪斟!
NSInteger interval = [zone secondsFromGMTForDate:datenow];
本地時(shí)區(qū) 與中央時(shí)區(qū)的時(shí)間差
NSDate?*localeDate?=?[datenow??dateByAddingTimeInterval:?interval];
通過(guò)計(jì)算這個(gè)時(shí)間差就是本地區(qū)現(xiàn)在的時(shí)間了
NSLog(@"%@",?localeDate);