NSDate

目錄
    1.1 初始化
    1.2 拆分時間
    1.3 搶購剩余時間
        1.3.1 時:分:秒
        1.3.2 年:月:日:時:分
    1.4 評論發(fā)布時間
    1.5 農(nóng)歷
    1.6 星期
1.1 初始化

NSDate *date = [NSDate date];

NSDate *yesterdayDate = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60)];

NSDateFormatter *format = [[NSDateFormatter alloc] init];

//設置日期的類型

[format setDateStyle:NSDateFormatterLongStyle];

//設置時間的類型

[format setTimeStyle:NSDateFormatterShortStyle];

//設置日期格式

[format setDateFormat:@"yyyy-MM-dd hh:mm:ss:SSS"];

NSString *s = [format stringFromDate:date];

NSLog(@"%@",s);

1.2 拆分時間

//得到當前時間

NSDate *date = [NSDate date];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

//實例化一個日期的對象,這個對象不是NSDate的是NSDateComponents的

NSDateComponents *com = [[NSDateComponents alloc] init];

//做一個標示趣倾,表示我們要什么內(nèi)容

NSInteger flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

//從一個日期里面把這些內(nèi)容取出來

com = [calendar components:flags fromDate:date];

NSInteger year = [com year];

NSInteger month = [com month];

NSInteger day = [com day];

NSInteger hour = [com hour];

NSInteger min = [com minute];

NSInteger sec = [com second];


NSLog(@"%ld",(long)year);

NSLog(@"%ld",(long)month);

NSLog(@"%ld",(long)day);

NSLog(@"%ld",(long)hour);

NSLog(@"%ld",(long)min);

NSLog(@"%ld",(long)sec);

1.3.1 搶購剩余時間

+ (NSString*)getExpireTime:(NSString*)date
{

    // 2016-07-26 13:12:20.0

    // 剩余:19:20:09

    // NSString->NSDate

    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];

    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.S";

    //字符串轉(zhuǎn)成date

    NSDate* expireDate = [formatter dateFromString:date];

    int second = [expireDate timeIntervalSinceNow];
  
    int h = second / 3600;

    int m = second % 3600 /60;

    int s = second % 60;

    return [NSString stringWithFormat:@"剩余:%02d:%02d:%02d",h,m,s];

}


    使用

    NSString *s = [JDDate getExpireTime:@"2016-07-26     13:12:20.0"];

    NSLog(@"%@",s);

    剩余:41:56:06
1.3.2 搶購剩余時間

+ (NSString*)getExpireTime:(NSString*)date
{

    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.S"];

    //結束時間
    NSDate* toDate = [dateFormatter dateFromString:@"2016-07-27 13:12:20.0"];

    //開始時間
    NSDate* startDate = [NSDate date];

    //實例化一個日歷 NSGregorianCalendar這個格式的日歷

    NSCalendar* chineseClendar = [[ NSCalendar alloc ] 
    initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSUInteger unitFlags = NSHourCalendarUnit | 
                         NSMinuteCalendarUnit | 
                         NSSecondCalendarUnit | 
                            NSDayCalendarUnit | 
                          NSMonthCalendarUnit |
                           NSYearCalendarUnit;

    NSDateComponents *cps = [chineseClendar components:unitFlags fromDate:startDate toDate:toDate options:0]

    NSInteger diffYear = [cps year];

    NSInteger diffMon = [cps month];

    NSInteger diffDay = [cps day];

    NSInteger diffHour = [cps hour];
  
    NSInteger diffMin = [cps minute];

    NSInteger diffSec = [cps second];

    return [NSString stringWithFormat:@"剩余: Years: %ld 
                                             Months: %ld,
                                               Days: %ld,
                                              Hours: %ld, 
                                               Mins:%ld, 
                                                sec:%ld",
              (long)diffYear, (long)diffMon, (long)diffDay, 
              (long)diffHour, (long)diffMin,(long)diffSec];

}


使用

NSString *s = [JJDate getExpireTime:@"2016-07-27 13:12:20.0"];

NSLog(@"%@",s);

結果:剩余: Years: 0 Months: 0, Days; 1, Hours: 18, Mins:31, sec:37
1.4 評論發(fā)布時間

+ (NSString *)getReleaseTime:(NSString *)releaseTime
{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    //dateFormat時間樣式屬性,傳入格式必須按這個
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.S";

    //locale:"區(qū)域欲险;場所"
    formatter.locale = [[NSLocale alloc]     initWithLocaleIdentifier:@"en_US"];

    //發(fā)布時間
    NSDate *date = [formatter dateFromString:releaseTime];

    //現(xiàn)在時間
    NSDate *now = [NSDate date];

    //發(fā)布時間到現(xiàn)在間隔多長時間,用timeIntervalSinceDate
    NSTimeInterval interval = [now timeIntervalSinceDate:date];

    NSString *format;

    if (interval <= 60) {

        format = @"剛剛";

    } else if(interval <= 60*60){

        format = [NSString stringWithFormat:@"發(fā)布于前%.f分鐘",interval/60];

    } else if(interval <= 60*60*24){

        format = [NSString stringWithFormat:@"發(fā)布于前%.f小時",interval/3600];

    } else if (interval <= 60*60*24*7){

        format = [NSString stringWithFormat:@"發(fā)布于前%d天", 
                 (int)interval/(60*60*24)];

    } else if (interval > 60*60*24*7 & interval <= 60*60*24*30 ){

        format = [NSString stringWithFormat:@"發(fā)布于前%d周", 
                 (int)interval/(60*60*24*7)];

    }else if(interval > 60*60*24*30 ){

        format = [NSString stringWithFormat:@"發(fā)布于前%d月", 
                 (int)interval/(60*60*24*30)];

  }

    return format;

}



使用

NSString *s = [JJDate getReleaseTime:@"2016-05-31 13:12:20.0"];

NSLog(@"%@",s);

結果:發(fā)布于前1天
1.5 農(nóng)歷

+ (NSString *) getChineseCalendarWithDate:(NSDate *) date
{
    /*天干名稱*/
    const char *cTianGan[] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
    /*地支名稱*/
    const char *cDiZhi[] = {"子","丑","寅","卯","辰","巳","午",
        "未","申","酉","戌","亥"};
    /*屬相名稱*/
    const char *cShuXiang[] = {"鼠","牛","虎","兔","龍","蛇",
        "馬","羊","猴","雞","狗","豬"};
    /*農(nóng)歷日期名*/
    const char *cDayName[] = {"*","初一","初二","初三","初四","初五",
        "初六","初七","初八","初九","初十",
        "十一","十二","十三","十四","十五",
        "十六","十七","十八","十九","二十",
        "廿一","廿二","廿三","廿四","廿五",
        "廿六","廿七","廿八","廿九","三十"};
    /*農(nóng)歷月份名*/
    const char *cMonName[] = {"*","正","二","三","四","五","六",
        "七","八","九","十","十一","臘"};
    
    /*公歷每月前面的天數(shù)*/
    const int wMonthAdd[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
    /*農(nóng)歷數(shù)據(jù)*/
    const int wNongliData[100] =
    {2635,333387,1701,1748,267701,694,2391,133423,1175,396438
        ,3402,3749,331177,1453,694,201326,2350,465197,3221,3402
        ,400202,2901,1386,267611,605,2349,137515,2709,464533,1738
        ,2901,330421,1242,2651,199255,1323,529706,3733,1706,398762
        ,2741,1206,267438,2647,1318,204070,3477,461653,1386,2413
        ,330077,1197,2637,268877,3365,531109,2900,2922,398042,2395
        ,1179,267415,2635,661067,1701,1748,398772,2742,2391,330031
        ,1175,1611,200010,3749,527717,1452,2742,332397,2350,3222
        ,268949,3402,3493,133973,1386,464219,605,2349,334123,2709
        ,2890,267946,2773,592565,1210,2651,395863,1323,2707,265877};
    
    static int wCurYear,wCurMonth,wCurDay;
    static int nTheDate,nIsEnd,m,k,n,i,nBit;
    
    //    char szNongli[30], szNongliDay[10],szShuXiang[10];
    NSString * szNongli = @"";
    NSString * szNongliMonth = @"";
    NSString * szShuXiang = @"";
    /*---取當前公歷年军浆、月乱豆、日---*/
    
    NSCalendar  * cal = [NSCalendar  currentCalendar];
    NSUInteger  unitFlags = NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
    NSDateComponents * conponent = [cal components:unitFlags fromDate:date];
    
    wCurYear = [conponent year];
    wCurMonth = [conponent month];
    wCurDay = [conponent day];
    
    /*---計算到初始時間1921年2月8日的天數(shù):1921-2-8(正月初一)---*/
    nTheDate = (wCurYear - 1921) * 365 + (wCurYear - 1921) / 4 + wCurDay + wMonthAdd
    [wCurMonth - 1] - 38;
    if((!(wCurYear % 4)) && (wCurMonth > 2))
        nTheDate = nTheDate + 1;
    /*--計算農(nóng)歷天干闲坎、地支崭闲、月吝岭、日---*/
    nIsEnd = 0;
    m = 0;
    while(nIsEnd != 1)
    {
        if(wNongliData[m] < 4095)
            k = 11;
        else
            k = 12;
        n = k;
        while(n>=0)
        {
            //獲取wNongliData(m)的第n個二進制位的值
            nBit = wNongliData[m];
            for(i=1;i<n+1;i++)
                nBit = nBit/2;
            nBit = nBit % 2;
            if (nTheDate <= (29 + nBit))
            {
                nIsEnd = 1;
                break;
            }
            nTheDate = nTheDate - 29 - nBit;
            n = n - 1;
        }
        if(nIsEnd)
            break;
        m = m + 1;
    }
    
    wCurYear = 1921 + m;
    wCurMonth = k - n + 1;
    wCurDay = nTheDate;
    
    if (k == 12)
    {
        if (wCurMonth == wNongliData[m] / 65536 + 1)
            wCurMonth = 1 - wCurMonth;
        else if (wCurMonth > wNongliData[m] / 65536 + 1)
            wCurMonth = wCurMonth - 1;
    }
    /*--生成農(nóng)歷天干三痰、地支、屬相 ==> wNongli--*/
    
    szShuXiang = [NSString stringWithCString:cShuXiang[((wCurYear - 4) % 60) % 12] encoding:NSUTF8StringEncoding];
    
    NSString * tempYear = [NSString stringWithCString:cTianGan[((wCurYear - 4) % 60) % 10] encoding:NSUTF8StringEncoding];
    NSString * tempDiZhi = [NSString stringWithCString:cDiZhi[((wCurYear - 4) % 60) % 12]  encoding:NSUTF8StringEncoding];
    szNongli = [NSString stringWithFormat:@"%@(%@%@)年",szShuXiang,tempYear,tempDiZhi];
    /*--生成農(nóng)歷月窜管、日 ==> wNongliDay--*/
    if (wCurMonth < 1) {
        NSString * tempMonth = [NSString stringWithCString:cMonName[-1 * wCurMonth] encoding:NSUTF8StringEncoding];
        szNongliMonth = [szNongliMonth stringByAppendingFormat:@"閏%@月",tempMonth];
    } else{
        NSString * tempMonth = [NSString stringWithCString:cMonName[wCurMonth] encoding:NSUTF8StringEncoding];
        szNongliMonth = [szNongliMonth stringByAppendingFormat:@"%@月",tempMonth];
    }
    
    NSString * tempDayName = [NSString stringWithCString:cDayName[wCurDay] encoding:NSUTF8StringEncoding];
    szNongli = [szNongli stringByAppendingFormat:@"%@%@",szNongliMonth,tempDayName];
    
    /*-------------修改自己想要的格式---------------*/
    NSString *myNongli = [NSString stringWithFormat:@"農(nóng)歷%@%@",szNongliMonth,tempDayName];
    return myNongli;
    /*-----------------修改結束-------------------*/
    //    return szNongli;
}

使用:
NSString *s = [ChineseCalendarDate getChineseCalendarWithDate:[NSDate new]];
1.6 星期

+(NSString *)getWeekAndTime:(NSDate *)date{

    NSCalendar * cal = [NSCalendar currentCalendar];

    NSUInteger unitFlags = NSYearCalendarUnit | 
                          NSMonthCalendarUnit | 
                            NSDayCalendarUnit | 
                        NSWeekdayCalendarUnit | 
                           NSHourCalendarUnit | 
                          NSMinuteCalendarUnit | 
                          NSSecondCalendarUnit;

    NSDateComponents * conponent = [cal components:unitFlags fromDate:date];

    NSInteger wCurWeek = [conponent weekday];

    NSString *week = nil;

    switch (wCurWeek) {

        case 1:
            week = @"日";
            break;
        case 2:
            week = @"一";
            break;
        case 3:
            week = @"二";
            break;
        case 4:
            week = @"三";
          break;
        case 5:
            week = @"四";
            break;
        case 6:
            week = @"五";
            break;
        case 7:
            week = @"六";
        default:
        break;
     }
    return week;
}

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末散劫,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子微峰,更是在濱河造成了極大的恐慌舷丹,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蜓肆,死亡現(xiàn)場離奇詭異颜凯,居然都是意外死亡,警方通過查閱死者的電腦和手機仗扬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門症概,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人早芭,你說我怎么就攤上這事彼城。” “怎么了退个?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵募壕,是天一觀的道長。 經(jīng)常有香客問我语盈,道長舱馅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任刀荒,我火速辦了婚禮代嗤,結果婚禮上棘钞,老公的妹妹穿的比我還像新娘。我一直安慰自己干毅,他們只是感情好宜猜,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著硝逢,像睡著了一般姨拥。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上趴捅,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天垫毙,我揣著相機與錄音,去河邊找鬼拱绑。 笑死,一個胖子當著我的面吹牛丽蝎,可吹牛的內(nèi)容都是我干的猎拨。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼屠阻,長吁一口氣:“原來是場噩夢啊……” “哼红省!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起国觉,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤吧恃,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后麻诀,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體痕寓,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年蝇闭,在試婚紗的時候發(fā)現(xiàn)自己被綠了呻率。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡呻引,死狀恐怖礼仗,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情逻悠,我是刑警寧澤元践,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站童谒,受9級特大地震影響单旁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜惠啄,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一慎恒、第九天 我趴在偏房一處隱蔽的房頂上張望任内。 院中可真熱鬧,春花似錦融柬、人聲如沸死嗦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽越除。三九已至,卻和暖如春外盯,著一層夾襖步出監(jiān)牢的瞬間摘盆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工饱苟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留孩擂,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓箱熬,卻偏偏與公主長得像类垦,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子城须,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容