Devang
Devang

Reputation: 11338

iPhone : Does not getting proper output while converting NSString into NSDate

I want to convert NSString into NSDate.

So I tried following code :

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-MM-yyyy"];

    NSString *checkInDate = lblCheckIn.text;
    NSString *checkOutDate = lblCheckOut.text;

Here I am getting following output.

    NSLog(@"\nCheckIn Date : %@\nCheckOut Date : %@",checkInDate,checkOutDate);
    // CheckIn Date : 26-10-2011
    // CheckOut Date : 29-10-2011

Now I want to convert this NSString into NSDate with dd-MM-yyyy formate.

    NSDate *checkOut = [formatter dateFromString:checkOutDate];
    NSDate *checkIn = [formatter dateFromString:checkInDate];
    [formatter release];

But I got following output.

    NSLog(@"\nCheck In : %@\nCheck Out : %@",checkIn,checkOut);
    // Check In : 2011-10-25 18:30:00 +000
    // Check Out : 2011-10-28 18:30:00 +000

What could be wrong ?

Upvotes: 0

Views: 200

Answers (2)

Srinivas
Srinivas

Reputation: 983

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[formatter setTimeZone:gmt];
NSString *checkInDate =@"26-10-2011";
NSString *checkOutDate =@"29-10-2011";
NSDate *checkOut = [formatter dateFromString:checkOutDate];
NSDate *checkIn = [formatter dateFromString:checkInDate];

    [formatter release];

Upvotes: 1

Ilanchezhian
Ilanchezhian

Reputation: 17478

I hope you are in +5:30 timezone.. Nothing wrong with your code. Its only printing the time in GMT timezone.

Upvotes: 2

Related Questions