IOS的日期差异 [英] Date Difference in IOS

查看:163
本文介绍了IOS的日期差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从日期到现在都有两个日期。我在NSString中分别存储了两个日期,如string1和string 2。现在我的问题是我想要这两个日期的区别。我的日期格式是 2014年3月4日 2014年3月14日,我希望结果像code> 10 。

解决方案

我认为,根据你的问题, 两个日期之间的差异。你可以用这个

  NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@dd-MM-yyyy];
NSDate * date1 = [dateFormat dateFromString:@04-Mar-2014];
NSDate * date2 = [dateFormat dateFromString:@2014年3月14日];

NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];

int numberOfDays = secondsBetween / 86400;

NSLog(@两个日期之间有%d天,numberOfDays);


I had Two Dates from date and to date. I stored two dates in NSString like string1 and string 2 respectively. Now My Problem is I want the difference of these two dates. My Date format is 04-Mar-2014 and 14-Mar-2014 and I want the result to be like 10.

解决方案

I think, according to your question, my assumption is difference between two dates. You can use this.

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];
NSDate *date1 = [dateFormat dateFromString:@"04-Mar-2014"];
NSDate *date2 = [dateFormat dateFromString:@"14-Mar-2014"];

NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];

int numberOfDays = secondsBetween / 86400;

NSLog(@"There are %d days in between the two dates.", numberOfDays);

这篇关于IOS的日期差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆