NSDate基础用法

来源:互联网 发布:淘宝点击图片跳转链接 编辑:程序博客网 时间:2024/06/05 22:55

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