59

I want current time in following format in a string.

dd-mm-yyyy HH:MM

How?

malexander
  • 4,522
  • 1
  • 31
  • 37
sagarkothari
  • 24,520
  • 50
  • 165
  • 235

4 Answers4

145

You want a date formatter. Here's an example:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm"];

NSDate *currentDate = [NSDate date];
NSString *dateString = [formatter stringFromDate:currentDate];
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Unless you want to keep it around; memory management may or may not enter into the problem. – Carl Norum Nov 06 '09 at 06:09
  • 6
    Date formatters are really slow to set up. If you are setting dates in a table cell or doing it a lot, as was mentioned above, you'll want to retain it, us it for everything, then release it in dealloc. – Steve Weller Nov 07 '09 at 06:08
  • 1
    @Moshe, did you try reading the documentation? `a` is the format string for AM/PM period. http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns – Carl Norum Jan 26 '12 at 23:19
  • @CarlNorum Yup, I did. A single a doesn't work. I'm going to post a question in a moment if I can't get it to work. EDIT: My mistake, I'm an idiot. – Moshe Jan 26 '12 at 23:23
15

Either use NSDateFormatter as Carl said, or just use good old strftime, which is also perfectly valid Objective-C:

#import <time.h>
time_t currentTime = time(NULL);
struct tm timeStruct;
localtime_r(&currentTime, &timeStruct);
char buffer[20];
strftime(buffer, 20, "%d-%m-%Y %H:%M", &timeStruct);
Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
  • Note that you can't do too much with a C string in Objective C. This might be more convenient when logging something to the console. You can just say `NSLog(@"%s", buffer)`. – Donovan Voss Nov 06 '09 at 05:27
  • 4
    Anything you can do with a C string in C, you can do in Objective-C. Which is to say, pretty much everything. One should feel free to use both C strings and NSStrings, and pick the appropriate one for the current use case. I personally would go with Carl's solution, but it's important to be aware that it's not the only way. – Stephen Canon Nov 06 '09 at 15:28
  • I would say the reason not to use strftime (despite it generating a C string that you then have to convert) is that you also lose out on the far greater flexibility of the NSDateFormatter date formatting options, including more variable length of numeric results and so on. – Kendall Helmstetter Gelner Nov 06 '09 at 23:06
  • this should be the preferred way on iOS becayuse of this bug http://stackoverflow.com/questions/143075/nsdateformatter-am-i-doing-something-wrong-or-is-this-a-bug , however i am finding that time() or localtime_r() are not always accurate, am seeing very weird results. – valexa Sep 12 '10 at 01:32
9

Here is a simple solution:

- (NSString *)stringWithDate:(NSDate *)date
{
  return [NSDateFormatter localizedStringFromDate:date
                                        dateStyle:NSDateFormatterMediumStyle
                                        timeStyle:NSDateFormatterNoStyle];
}

Change the dateStyle and timeStyle to match your formatting requirement.

Zorayr
  • 23,770
  • 8
  • 136
  • 129
3

Maybe this will be more readable :

    NSDateFormatter *date = [[NSDateFormatter alloc] init];
    [date setDateFormat:@"HH:mm"];
    NSString *dateString = [date stringFromDate:[NSDate date]];
    [self.time setText:dateString];

First of all we create an NSDateFormatter built-in in obj-c with the name date, then we apply it by [[NSDateFormatter alloc] init]; . After that we say to the code procesor that we want our date to have HOUR/MINUTE/SECOND. Finally we should make our date to be an string to work with alert or set value of a label , to do this we should create an string with NSString method then we use this : [date stringFromDate:[NSDate date]]

Have Fun with It .