1

I am receiving the following date in string format from the API

2015-04-18 06:08:28.000000

I want the date to be in the format of d/M/yyyy

I tried the following

NSString *datevalue = (NSString*)value;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"d/M/yyyy"];
NSDate *date =  [formatter dateFromString:datevalue];
NSString *currentDate = [formatter stringFromDate:date];

This returns NIL, what could be the possible issue, or how do i format such dates in objective-c?

Thanks.

Black Frog
  • 11,595
  • 1
  • 35
  • 66
Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
  • 1
    Do you have any control over the API's format for dates? Often RFC 3339/ISO 8601 dates include a `Z` at the end (to make it explicit that it is GMT/UTC/Zulu). And often there is a `T` between the date and year. You can use whatever format you want, but a common convention would be `2015-04-18T06:08:28.000000Z`. – Rob Apr 19 '15 at 18:03
  • The format string you use to read the character date should look vaguely like the string you're reading!! http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns – Hot Licks Apr 19 '15 at 23:59
  • Thanks @Rob, I have control over the API server, I'll have a look – Ibrahim Azhar Armar Apr 20 '15 at 03:25

3 Answers3

1

You cannot use the same formatter for read and writing the date string because they are the different. The format for your input date is incorrect.

// input string date: 2015-04-18 06:08:28.000000
// [formatter setDateFormat:@"d/M/yyyy"]; // incorrect
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSSSS"]; 

Below is sample code

//
//  main.m
//  so29732496
//
//  Created on 4/19/15.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"Hello, World!");

        NSString *dateStringFromAPI = @"2015-04-18 06:08:28.000000";
        NSString * const kAPIDateFormat = @"yyyy-MM-dd HH:mm:ss.SSSSSS";

        // convert API date string
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:kAPIDateFormat];
        NSDate *apiDate =  [formatter dateFromString:dateStringFromAPI];


        // now if I was output the api date to another format
        // I have to change the formatter
        [formatter setDateFormat:@"dd/M/yyyy"];
        NSString *currentDate = [formatter stringFromDate:apiDate];

        NSLog(@"Current Date: %@", currentDate);
    }
    return 0;
}
Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • 1
    I used the wrong formatter for milliseconds. It's capital S's. – Black Frog Apr 19 '15 at 16:48
  • Yes just like I wrote in my answer. If it worked out for you @IbrahimAzharArmar I would appreciate marking the answer as accepted since this way people who face the same problem can find the way to go easily. But I mean you have 8000 reputation points I think you know that by yourself, just a reminder. – dehlen Apr 19 '15 at 17:11
1

Just want to add to Black Frog's answer: As he stated you need different formatters for reading/writing.

However the right format should be :

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSSSS"];

According to the apple documentation fractional seconds should be formatted with 'S'.

See here: NSDateFormat

Also here is an example to accomplish your task:

    NSString *datevalue = @"2015-04-18 06:08:28.000000";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSSSS"];
    NSDate *date =  [formatter dateFromString:datevalue];
    [formatter setDateFormat:@"dd/MM/yyyy"];
    NSString *currentDate = [formatter stringFromDate:date];

    NSLog(@"%@",date);
    NSLog(@"%@",currentDate);
dehlen
  • 7,325
  • 4
  • 43
  • 71
1

You should use the yyyy-MM-dd HH:mm:ss.SSSSSSS format string for the date formatter converting the API date into a NSDate object, as discussed by the others. But, you also want to consider the timeZone and locale properties of this formatter.

  • Usually RFC 3339 dates are exchanged in GMT. Confirm this with your API, but it's generally GMT/UTC/Zulu. If so, you will probably also want to explicitly set the timezone:

    formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    

    But confirm what timezone the API expects.

  • A more subtle issue is the handling users with non-Gregorian calendars

    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    

For more information, see Apple Technical Q&A 1480.

Clearly, these dateFormat, timeZone, and locale properties are simply for converting the API date string into a NSDate object. When then outputting the date for the end user, you would use a separate formatter, defaulting to the standard timeZone and locale properties and use whatever dateFormat string you want for the output. (Frankly, I wouldn't generally advise using dateFormat string for user output formatters, but rather just use the appropriate values for the dateStyle and timeStyle properties.)

Rob
  • 415,655
  • 72
  • 787
  • 1,044