How do I convert the following Objective-C code into Swift code?
#define MAX_SIZE 11
char buffer[MAX_SIZE];
time_t time = [[NSDate date] timeIntervalSince1970];
strftime(buffer, MAX_SIZE, "%-l:%M\u2008%p", localtime(&time));
NSString *dateString = [NSString stringWithUTF8String:buffer];
NSLog(@"dateString: %@", dateString); // dateString: 11:56 PM
I'm formatting a date
.
My function i use.
As the commentators @BryanChen and @JasonCoco said, use NSDateFormatter.
A full description of the format strings is available in "Data Formatting Guide".
Here is another example that uses
NSDateFormatterStyle
:The output is formatted as, "January 1, 1990".
If you want to read more about the formatter and the different available styles, checkout, NSFormatter under NSDateFormatter section.