溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

IOS獲取當前時間

發布時間:2020-07-18 00:42:45 來源:網絡 閱讀:653 作者:江山風雨 欄目:移動開發

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);



獲取若干天前的日期:

NSTimeInterval secondsPerDay = 24 * 60 * 60;

        for (int i = 0; i < 400; i++) {

            NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-i*secondsPerDay];

            NSLog(@"%@", date);

        }


附件:http://down.51cto.com/data/2363334
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女