2

I have the following code which takes a NSString and returns NSDate. I have copied this code from a project in which it runs perfectly fine - but some how this gives me the wrong output

- (NSDate *)dateFromString:(NSString *)date
{
    static NSDateFormatter *dateFormatter;
    if (!dateFormatter)
    {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    }

    NSLog(@"Date: %@ Formatted: %@",date,[dateFormatter dateFromString:date]);

    return [dateFormatter dateFromString:date];
}

and the output is from logs:

Date: 07-01-2014 Formatted: 2014-01-06 18:30:00 +0000
Date: 24-01-2014 Formatted: 2014-01-23 18:30:00 +0000
Date: 06-01-2014 Formatted: 2014-01-05 18:30:00 +0000
Date: 15-01-2014 Formatted: 2014-01-14 18:30:00 +0000
Date: 22-01-2014 Formatted: 2014-01-21 18:30:00 +0000
Date: 31-01-2014 Formatted: 2014-01-30 18:30:00 +0000
Date: 14-01-2014 Formatted: 2014-01-13 18:30:00 +0000
Date: 30-01-2014 Formatted: 2014-01-29 18:30:00 +0000

In a weird sense it is also changing the Date..!! Any help...!!!

Magnus
  • 2,016
  • 24
  • 32
ankit_rck
  • 1,796
  • 2
  • 14
  • 24

2 Answers2

7

The results are formated for GMT output - i.e. if you are 5:3 hours away from GMT.

You need to specify a timezone if you want the date formated to be returned in that timezone: (that is what the +0000 means)

- (NSDate *)dateFromString:(NSString *)date
{
    static NSDateFormatter *dateFormatter;
    if (!dateFormatter)
    {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    }

    NSLog(@"Date: %@ Formatted: %@",date,[dateFormatter dateFromString:date]);

    return [dateFormatter dateFromString:date];
}
Magnus
  • 2,016
  • 24
  • 32
  • Date: 01-01-2014 Formatted: 2014-01-01 00:00:00 +0000 Why am i getting this in the output...!!! Shouldnt it be just 01-01-2014? – ankit_rck Jan 17 '14 at 08:22
  • the +0000 ? it's the timezone – Magnus Jan 17 '14 at 08:23
  • i just need the date-month-year as in 01-01-2014. i dont need the hour and timezone. How an i remove that – ankit_rck Jan 17 '14 at 08:24
  • Then you need to format the output - your method only takes a string and makes it to a NSDate object... see http://stackoverflow.com/questions/576265/convert-nsdate-to-nsstring – Magnus Jan 17 '14 at 08:27
  • When you `NSLog` an `NSDate`, it always outputs the whole date and timezone. If you only wish to output part of the date, you'll need to convert it _back_ into a string. – James Frost Jan 17 '14 at 10:08
-1

Check this

- (NSDate *)dateFromString:(NSString *)date
{
    static NSDateFormatter *dateFormatter;
    if (!dateFormatter)
    {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    }
    NSDate *dateValue= [dateFormatter dateFromString:date];
    NSLog(@"Date: %@ Formatted: %@",date,[dateFormatter stringFromDate:dateValue]);

    return dateValue;
}
Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30