XCODE IOS 5 - Date Format

Posted on 2012-08-23
Last Modified: 2012-08-24
Hi Experts,

How would I format the following date in xcode:



dd, MMM yyyy?

Question by:rmartes

    Author Comment

    Anyone care to give a shot? I tried the normal ios dateformatter similar to:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
    NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];
    NSString *formattedDateString = [dateFormatter stringFromDate:date];
    NSLog(@"formattedDateString: %@", formattedDateString);

    But no luck
    LVL 33

    Expert Comment

    I just tried and don't solve the question.

    In this way:
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH-mm-ss'Z'"];
            NSString *test = [dateFormatter stringFromDate:[NSDate date]];
            NSLog(@"the date is %@", test);

    Open in new window

    I get the string:
    the date is 2012-08-24T12-52-14Z
    LVL 14

    Expert Comment

    by:Hamidreza Vakilian
    You mean you want to parse a string like "2012-08-21T19:29:52Z" and then convert it to dd, MMM yyyy format? If yes you can easily separate the input string (tokenize it) and pass it to NSDate to get the date components.

    Author Comment

    Thanks guys...

    Programmer-x, Yes that's exactly what I want to do....but how would I do that?
    LVL 33

    Accepted Solution

    So the following code works:

           NSString *input = @"2012-08-21T19:29:52Z";
            NSString *modified = [input stringByReplacingOccurrencesOfString:@"T" withString:@""];
            modified = [modified stringByReplacingOccurrencesOfString:@"Z" withString:@""];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
            NSDate *output = [dateFormatter dateFromString:modified];
            NSLog(@"the date is %@", output);

    Open in new window

    The output:
    2012-08-24 18:59:51.854 DateTimeFormatTest[71926:403] the date is 2012-08-21 16:29:52 +0000

    Author Closing Comment

    Great!! Thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    Title # Comments Views Activity
    iPhone - Forwarding of Messages 4 23
    Unit test ionic mobile app for iPad 2 78
    "Reset" on Apple devices 2 50
    iOS10 Features 2 33
         Intro      In this article, I will show you how to parse a JSON in Swift using an open source library called SwiftyJSON ( If you haven't heard of a JSON before, it stands for JavaScript Object Notation…
    The release of the iPhone 6 and the iPhone 6 Plus was extremely successful for Apple. However when iOS 8.1 was introduced, many iPhone users complained of quick battery drainage. Without even knowing, you may be unnecessarily using up your battery p…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now