开发者

Convert String to Date and fetch the difference

hi i have 2 dates in string format

base_date_string = 10-12-01 12:00:00 current_date_string = 10-12-23 10:18:00 both the above values are in string

i want to get the number of days elapsed between these 2 dates

I tried to convert them to NSDate using NSDateFormatters and then getting the difference. I realised that string does not properly converts to NSDate

when i convert to nsdate i got base_date:::2010-12-01 06:30:00 +0000 current_date::::2010-12-23 04:48:19 +0000 (the time portion is not perfect)

Formatter class that i used is:

    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    [formatter1 setDateFormat:@"yy-MM-dd HH:mm:ss"];
    NSDate *base_date = [formatter1 dateFromString:@"10-12-01 12:00:00"];
    [formatter1 release];

    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"yy-MM-dd HH:mm:ss"];
    NSDate *current_date = [formatter2 dateFromString:current_date_string];
    [formatter2 release];

//subrtrcation of basedate from current date to get elapsed number of days

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *diff = [calendar components:(NSDayCalendarUnit) 开发者_运维技巧                      
                         fromDate:base_date toDate:current_date options:0];
int date_value = [diff day];

Please any help is appreciated


30 seconds in the NSDate documentation revealed:

-[NSDate timeIntervalSinceDate:]

So using the dates in your question...

NSTimeInterval difference = [current_date timeIntervalSinceDate:base_date];
difference = fabs(difference);
NSLog(@"there are %f seconds between %@ and %@", difference, current_date, base_date);

edit

ok, so the problem is not date differencing. You're observing that the string you're inputting is 5 and 1/2 hours ahead of the date you're getting back.

Well, let's look at this. The date returned is in GMT time (as denoted by the +0000). 5 and 1/2 hours ahead of that is the timezone used in India. So. Are you in India? If you are, then this is just a matter of needing to -setTimezone: on your NSDateFormatter.


You can use this code of function to get the difference between 2 dates

-(int)howManyDaysHavePast:(NSDate*)lastDate :(NSDate*)today {
NSDate *startDate = lastDate;
NSDate *endDate = today;
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
                                            fromDate:startDate
                                              toDate:endDate options:0];
[gregorian release];
int days = [components day];
return days;
}

hAPPY iCODING...


Use the following code

NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"yy-MM-dd HH:mm:ss"];
NSDate *base_date = [formatter1 dateFromString:@"10-12-01 12:00:00"];

NSDate *current_date = [formatter2 dateFromString:current_date_string];
[formatter1 release];

NSTimeInterval difference = [current_date timeIntervalSinceDate:base_date];

Then you will get difference in number of seconds. Then you can get in number of days as following

float days = difference/86400;

The "days" consists the number of days that the current_date is differ from the base_date.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜