Augustin Jose
Augustin Jose

Reputation: 400

DateFormatter for format : 2014-12-08T14:11:32.636Z

I have a complex date format and I need to process it to only date and time. Example for my current date format is given below.

current format: 2014-12-08T14:11:32.636Z

I am not sure what some fields are meant to be. What I want from above example is given below.

firstString = "08-12-2014"
secondString = "14:11"

I already tried many date formats but they didn't worked. So please help me. Thanks for your time.

Upvotes: 1

Views: 150

Answers (2)

Alex Andrews
Alex Andrews

Reputation: 1498

// Date formatter for your date string: 2014-12-08T14:11:32.636Z
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];

    // Date from your string
    NSDate *date = [dateFormatter dateFromString:@"2014-12-08T14:11:32.636Z"];

    // T => Week Day
    // S => Fractional Second
    // Z => zone- Time Zone


    // Updating to your format for preffered style
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSString *firstString = [dateFormatter stringFromDate:date];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *secondString = [dateFormatter stringFromDate:date];

For more ref: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Upvotes: -1

Sarath Neeravallil
Sarath Neeravallil

Reputation: 366

- (NSDate *)formatStringToDate:(NSString *)string
{
    NSDateFormatter *dateFormatter=[NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    NSDate *date = [dateFormatter dateFromString:string];
    return date;
}

Try this will work...

Upvotes: 2

Related Questions