NSDate

目录
    1.1 初始化
    1.2 拆分时间
    1.3 抢购剩余时间
        1.3.1 时:分:秒
        1.3.2 年:月:日:时:分
    1.4 评论发布时间
    1.5 农历
    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];

//做一个标示,表示我们要什么内容

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

//从一个日期里面把这些内容取出来

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

    //字符串转成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 评论发布时间

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

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

    //dateFormat时间样式属性,传入格式必须按这个
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.S";

    //locale:"区域;场所"
    formatter.locale = [[NSLocale alloc]     initWithLocaleIdentifier:@"en_US"];

    //发布时间
    NSDate *date = [formatter dateFromString:releaseTime];

    //现在时间
    NSDate *now = [NSDate date];

    //发布时间到现在间隔多长时间,用timeIntervalSinceDate
    NSTimeInterval interval = [now timeIntervalSinceDate:date];

    NSString *format;

    if (interval <= 60) {

        format = @"刚刚";

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

        format = [NSString stringWithFormat:@"发布于前%.f分钟",interval/60];

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

        format = [NSString stringWithFormat:@"发布于前%.f小时",interval/3600];

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

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

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

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

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

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

  }

    return format;

}



使用

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

NSLog(@"%@",s);

结果:发布于前1天
1.5 农历

+ (NSString *) getChineseCalendarWithDate:(NSDate *) date
{
    /*天干名称*/
    const char *cTianGan[] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
    /*地支名称*/
    const char *cDiZhi[] = {"子","丑","寅","卯","辰","巳","午",
        "未","申","酉","戌","亥"};
    /*属相名称*/
    const char *cShuXiang[] = {"鼠","牛","虎","兔","龙","蛇",
        "马","羊","猴","鸡","狗","猪"};
    /*农历日期名*/
    const char *cDayName[] = {"*","初一","初二","初三","初四","初五",
        "初六","初七","初八","初九","初十",
        "十一","十二","十三","十四","十五",
        "十六","十七","十八","十九","二十",
        "廿一","廿二","廿三","廿四","廿五",
        "廿六","廿七","廿八","廿九","三十"};
    /*农历月份名*/
    const char *cMonName[] = {"*","正","二","三","四","五","六",
        "七","八","九","十","十一","腊"};
    
    /*公历每月前面的天数*/
    const int wMonthAdd[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
    /*农历数据*/
    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日的天数:1921-2-8(正月初一)---*/
    nTheDate = (wCurYear - 1921) * 365 + (wCurYear - 1921) / 4 + wCurDay + wMonthAdd
    [wCurMonth - 1] - 38;
    if((!(wCurYear % 4)) && (wCurMonth > 2))
        nTheDate = nTheDate + 1;
    /*--计算农历天干、地支、月、日---*/
    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;
    }
    /*--生成农历天干、地支、属相 ==> 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];
    /*--生成农历月、日 ==> 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:@"农历%@%@",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;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,386评论 6 479
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,939评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,851评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,953评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,971评论 5 369
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,784评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,126评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,765评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,148评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,744评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,858评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,479评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,080评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,053评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,278评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,245评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,590评论 2 343

推荐阅读更多精彩内容