XCODE IOS 5 - Date Format

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

How would I format the following date in xcode:



dd, MMM yyyy?

Question by:rmartes
  • 3
  • 2

Author Comment

ID: 38326988
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

ID: 38328584
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 15

Expert Comment

by:Hamidreza Vakilian
ID: 38328611
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.
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.


Author Comment

ID: 38329457
Thanks guys...

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

Accepted Solution

pgnatyuk earned 2000 total points
ID: 38329950
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

ID: 38330100
Great!! Thanks

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
While there are many new features for iOS 11, these are the five that can improve your digital lifestyle.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Suggested Courses

862 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