我需要找到我的iphone應用程序的當前時間。我需要把它分解成不同的單位,所以我需要不同變量的小時,分鍾和秒。請幫忙!尋找iphone編程的時間
3
A
回答
0
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH"];
int hour = [[dateFormatter stringFromDate:[NSDate date]] intValue];
[dateFormatter setDateFormat:@"mm"];
int minute = [[dateFormatter stringFromDate:[NSDate date]] intValue];
[dateFormatter setDateFormat:@"ss"];
int second = [[dateFormatter stringFromDate:[NSDate date]] intValue];
[dateFormatter release];
+0
這個工作,但'NSCalendar'和'NSDateComponents'是要走的路。 –
+0
你也可以格式化「HHmmss」或其他一些,然後子字符串。由於您必須先創建日曆,因此NSDateComponents有點笨拙。 –
4
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
[calendar release];
1
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
NSLog(@"%ld:%ld:%ld", hour, minute, second);
23:34:6
相關問題
- 1. 設置在AVQueuePlayer中尋找時間iphone
- 2. 尋找時間AVQueuePlayer
- 3. 尋找iPhone的瀏覽器空間
- 4. iPhone尋找AppDelegate
- 5. 尋找iphone應用程序的視頻
- 6. 尋找時間複雜
- 7. 尋找例子用iPhone編寫的用於iPhone的GUI應用程序
- 8. 在iPhone編程中發光的時間?
- 9. 尋找添加時間空間
- 10. 尋找編寫PHP模塊的教程?
- 11. 尋找Linux的鼠標編程用C
- 12. 尋找關於編程的QR碼
- 13. 的MySQL - 尋找時間重疊
- 14. 的Python 3.5.0時間模塊:尋找
- 15. 尋找OpenGL的垂直回掃時間
- 16. 使用NETWORK_PROVIDER尋找位置的時間
- 17. 尋找最大價值爲r的時間序列數據編程
- 18. 尋找遺傳編程框架/庫
- 19. 在Java中尋找GCD,Socket編程
- 20. 複數 - 尋找theta - C編程
- 21. 在C編程中尋找平方根
- 22. 在AutoHotkey中尋找線程編號
- 23. 尋找微型可編程FPGA +機器
- 24. 尋找時間序列數據的總時間不重疊
- 25. 尋找編碼的類型
- 26. 尋找一個JavaFX時間表圖表
- 27. 尋找分期時間複雜度
- 28. 尋找能夠在指定時間
- 29. 使用Hibernate @Formula尋找時間差
- 30. 尋找最短分度時間Solr中
你得到這個工作? – bryanmac