码迷,mamicode.com
首页 > 其他好文 > 详细

NSDate 总结日期操作

时间:2014-11-21 21:51:33      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   os   sp   for   on   

IOS Object-c NSDate总结日期操作

 //NSDate

    //1, 创建NSDate对象

    NSDate *nowDate = [NSDate date];

    NSLog(@"%@",nowDate);

    

    //2, 创建明天现在的时间

    NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:24*3600];

     NSLog(@"%@",tomorrow);

    

    //3, 创建昨天现在的时间

    NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-24*3600];

    NSLog(@"%@",yesterday);

 

    //4, 间隔

    NSTimeInterval interval = [tomorrow timeIntervalSinceDate:yesterday];

    NSLog(@"间隔%.0f",interval/3600);

 

    

    //计算当前时间和?一个固定时间的差值,如果差值在60秒内,输出“刚 刚”,如果在60秒外3600秒内,输出“xx分钟前”,如果3600秒外, 3600*24秒内,输出“xx?小时前”。

     NSDate *t1 = [NSDate dateWithTimeIntervalSinceNow:10];

    //

    NSTimeInterval time2 = [t1 timeIntervalSinceNow];

    

    if (time2 <= 60) {

        NSLog(@"刚刚");

    }else if (time2 <= 3600 && time2 > 60){

        NSLog(@"%f",time2/3600);

    }else{

        NSLog(@"%f",time2);

    }

    

    

    

    

    //创建时间格式化类

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

    //设置样式

    [dateFormatter setDateFormat:@"yyyy年MM月dd日 hh时mm分ss秒"];

    //

    NSString *dateString = [dateFormatter stringFromDate:nowDate ];

    NSLog(@"%@",dateString);

    

    

    


 

1 // 当前时间创建NSDate

        NSDate *myDate = [NSDate date];

        NSLog(@"myDate = %@",myDate);

2 //从现在开始的24小时

        NSTimeInterval secondsPerDay = 24*60*60;

        NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];

        NSLog(@"myDate = %@",tomorrow);

3//根据已有日期创建日期

         NSTimeInterval secondsPerDay1 = 24*60*60;

        NSDate *now = [NSDate date];

        NSDate *yesterDay = [now addTimeInterval:-secondsPerDay1];

        NSLog(@"yesterDay = %@",yesterDay);

 

 4//比较日期

        BOOL sameDate = [now isEqualToDate:yesterDay];

        NSLog(@"sameDate = %lu",sameDate);

        4.1//获取较早的日期

        NSDate *earlierDate = [yesterDay earlierDate:now];

        NSLog(@"earlierDate  = %@",earlierDate);

        4.2//较晚的日期

        NSDate *laterDate = [yesterDay laterDate:now];

        NSLog(@"laterDate  = %@",laterDate);

 

        //两个日期之间相隔多少秒

        NSTimeInterval secondsBetweenDates= [yesterDay timeIntervalSinceDate:now];

        NSLog(@"secondsBetweenDates=  %lf",secondsBetweenDates);

        //通过NSCALENDAR类来创建日期

        NSDateComponents *comp = [[NSDateComponentsalloc]init];

        [comp setMonth:06];

        [comp setDay:01];

        [comp setYear:2001];

        NSCalendar *myCal = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];

        NSDate *myDate1 = [myCal dateFromComponents:comp];

        NSLog(@"myDate1 = %@",myDate1);

 

        //从已有日期获取日期

        unsigned units  = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;

        NSDateComponents *comp1 = [myCal components:units fromDate:now];

        NSInteger month = [comp1 month];

        NSInteger year = [comp1 year];

        NSInteger day = [comp1 day];

        //NSDateFormatter实现日期的输出

        NSDateFormatter *formatter = [[NSDateFormatteralloc]init];

        [formatter setDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码

        //或者是手动设置样式[formatter setDateFormat:@"yyyy-mm-dd"];

        NSString *string = [formatter stringFromDate:now];

        NSLog(@"string = %@",string);

        NSLog(@"formater = %@",formatter);

 

 

//获取日期格式对象

- (NSDateFormatter *)dateFormatter {

if (dateFormatter == nil) {

dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

}

return dateFormatter;

NSDate 总结日期操作

标签:style   blog   http   io   ar   os   sp   for   on   

原文地址:http://www.cnblogs.com/iOS-mt/p/4113688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!