歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS獲取當前時間

iOS獲取當前時間

日期:2017/3/1 9:54:10   编辑:Linux編程

NSDate

1.NSDate對象用來表示一個具體的時間點。

2.NSDate是一個類簇,我們使用的NSDate對象都是它的私有子類的實體。

3.NSDate存儲的是GMT時間,使用的時候會根據 當前應用 指定的 時區 進行時間上的增減,以供計算或顯示。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now;
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSWeekdayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit;
now=[NSDate date];
comps = [calendar components:unitFlags fromDate:now];

NSInteger year = [comps year];
NSInteger month = [comps month];
NSInteger day = [comps day];
NSInteger hour = [comps hour];
NSInteger min = [comps minute];
NSInteger sec = [comps second];
NSLog(@"year:%ld \n \
month:%ld \n \
day:%ld \n \
hour:%ld \n \
min:%ld \n \
sec:%ld",

year,
month,
day,
hour,
min,
sec);

//組裝
NSString *stringDate = [NSString stringWithFormat:@"%ld%ld%ld%ld%ld%ld",
year,
month,
day,
hour,
min,
sec

];

NSLog(@"stringDate:%@", stringDate);

iOS獲取當前時間 源代碼

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2013年資料8月/14日/iOS獲取當前時間

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

Copyright © Linux教程網 All Rights Reserved