Ben
Ben

Reputation: 969

Conversion of NSString to NSDate conversion - incorrect result

In short words I plan to get current dateTime, change the time and make it local to Malaysia Time by applying +0800 to timezone.

The result is unexpected :

-(NSDate *)departureDateTime
{
    NSDate *date = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
    [components setHour: 7];
    [components setMinute: 59];
    [components setSecond: 17];

    NSDate *newDate = [gregorian dateFromComponents: components];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-mm-dd HH:mm:ss Z"];
    [dateFormat setLocale:[NSLocale currentLocale]];


    NSString *newDateString = [NSString stringWithFormat:@"%@",newDate];
    NSString *maskString = [NSString stringWithFormat:@"%@", [newDateString substringToIndex:20]];
    NSString *append = [maskString stringByAppendingString:@"+0800"];
    NSLog(@"%@",append);


    NSDate *finalLocalDate = [dateFormat dateFromString:append];

    return finalLocalDate;
}

Results :

for NSLog Append : 2013-12-07 23:59:17 +0800

but finalLocalDate : 2013-01-07 15:59:17 +0000

Upvotes: 0

Views: 165

Answers (3)

Hot Licks
Hot Licks

Reputation: 47729

If your computer is running in the correct timezone don't set a timezone.

Create your newDate value and then --

NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSString* printableDate = [fmt stringFromDate:newDate];

NSLog(@"The date is %@", printableDate);

Only set a timezone (in both NSCalendar and NSDateFormatter) if the desired timezone is not the one your computer/phone is currently using.

Note that if you NSLog newDate directly it will print in GMT timezone. This is the way it's supposed to be -- you always use NSDateFormatter for a printable date. When you NSLog an NSDate directly you get GMT, by design.

Upvotes: 0

Ben
Ben

Reputation: 969

Found the answer with much shorter solution, so I posted here in case it helps anyone in future.

for returning, the problem was different time zones so by adding this line of

[components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:(+0*3600) ] ];

we set the timezone to system time zone then we remove unnecessary codes :

-(NSDate *)departureDateTime
{
    NSDate *date = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];

    [components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:(+0*3600) ] ];
    [components setHour: 7];
    [components setMinute: 59];
    [components setSecond: 17];

    NSDate *newDate = [gregorian dateFromComponents: components];
    NSLog(@"%@",newDate);

    return newDate;
}

Correct Result : 2013-12-08 07:59:17 +0000

Upvotes: 1

thorb65
thorb65

Reputation: 2716

try this:

NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
NSDate *malaysianTime = [NSDate dateWithTimeIntervalSince1970:now+(8*60*60)]; //8h in seconds

Upvotes: 0

Related Questions