关闭

NSDate相关

标签: nsdate
474人阅读 评论(0) 收藏 举报
   //获得当前日期
        NSDate *now = [NSDate date];
        NSCalendar *cal = [NSCalendar currentCalendar];
        unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit;
        NSDateComponents *dd = [cal components:unitFlags fromDate:now];
        int y = [dd year];
        int m = [dd month];
        int d = [dd day];
        int hour = [dd hour];
        int minute = [dd minute];

        NSString * createTime = [NSString stringWithFormat:@"%d/%02d/%02d %02d:%02d",y,m,d,hour,minute];//
//以下计算时间间隔转自 http://blog.csdn.net/wangqiuyun/article/details/8128918
//创建日期格式化对象
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];

//创建了两个日期对象
NSDate *date1=[dateFormatter dateFromString:@"2010-3-3 11:00"];
NSDate *date2=[dateFormatter dateFromString:@"2010-3-4 12:00"];
//NSDate *date=[NSDate date];
//NSString *curdate=[dateFormatter stringFromDate:date];

//取两个日期对象的时间间隔:
//这里的NSTimeInterval 并不是对象,是基本型,其实是double类型,是由c定义的:typedef double NSTimeInterval;
NSTimeInterval time=[date2 timeIntervalSinceDate:date1];

int days=((int)time)/(3600*24);
int hours=((int)time)%(3600*24)/3600;
NSString *dateContent=[[NSString alloc] initWithFormat:@"%i天%i小时",days,hours];





   
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:107780次
    • 积分:1796
    • 等级:
    • 排名:千里之外
    • 原创:64篇
    • 转载:48篇
    • 译文:6篇
    • 评论:10条
    最新评论