溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

NSdate簡(jiǎn)單使用

發(fā)布時(shí)間:2020-07-03 15:42:00 來源:網(wǎng)絡(luò) 閱讀:459 作者:Im劉亞芳 欄目:開發(fā)技術(shù)
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
    @autoreleasepool {
        //返回當(dāng)前日期
        NSDate  *dat = [NSDate date]; //格林尼治時(shí)間,和北京時(shí)間相差8個(gè)小時(shí)
        NSLog(@"%@", dat);
        //距離當(dāng)前日期多少秒之后的日期
        NSDate *sincenew = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60)];  //后面的參數(shù)是秒,計(jì)算都是以秒來計(jì)算的 。
        NSLog(@"%@", sincenew);
        
        NSDate *a = [NSDate dateWithTimeInterval:24*60*60 sinceDate:sincenew];
        NSLog(@"===%@",a);
        //返回從2001.1.1添加描述日期
        NSDate *sinceDate = [NSDate dateWithTimeIntervalSinceReferenceDate:60*24*60*(31+28+31+30+31+30+31)*31+24*60*60*2];
        NSLog(@"===%@",sinceDate);
        
        //從 1970-01-01開始計(jì)算時(shí)間
        NSDate *b = [NSDate dateWithTimeIntervalSince1970:60];
        NSLog(@"%@", b);
        
        NSTimeInterval time = [sinceDate timeIntervalSinceNow];
        NSLog(@"%f",time);
        //創(chuàng)建NSDateFormatter
       
        NSDateFormatter *aa = [[NSDateFormatter alloc] init];
//        [aa setDateStyle:kCFDateFormatterShortStyle];  時(shí)間格式
        [aa setDateFormat:@"YYYY-MM-dd aaaa KK:mm:ss"];  //返回當(dāng)前日期 aaaa表示上午
        NSString *newStr = [aa stringFromDate:dat];
        NSLog(@"%@",newStr);
        // 創(chuàng)建日期對(duì)象,NSTimeINterval
        //使用NSDateFormatter進(jìn)行字符串和日期轉(zhuǎn)化
          //1.創(chuàng)建NSDtaeFormatter對(duì)象
          //2.設(shè)置日期格式,使用[setDateFormat]
        //3.使用[stringFromDate]和[dateFromString]
            }
    return 0;
}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI