Skip to content

Instantly share code, notes, and snippets.

@software-mariodiana
Last active October 30, 2020 16:45
Show Gist options
  • Save software-mariodiana/132c579bc1d56caab74a174d8edeea2a to your computer and use it in GitHub Desktop.
Save software-mariodiana/132c579bc1d56caab74a174d8edeea2a to your computer and use it in GitHub Desktop.
Category on NSDate to print timestamp in ISO-8601 format.
#import <Foundation/Foundation.h>
@interface NSDate (ISO8601)
- (NSString *)mdx_canonicalTimestamp;
@end
@implementation NSDate (ISO8601)
- (NSString *)mdx_canonicalTimestamp
{
static NSISO8601DateFormatter* formatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
formatter = [[NSISO8601DateFormatter alloc] init];
formatter.formatOptions = NSISO8601DateFormatWithInternetDateTime;
});
return [formatter stringFromDate:self];
}
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
NSDate* now = [NSDate date];
NSLog(@"%@", [now mdx_canonicalTimestamp]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment