Skip to content

Instantly share code, notes, and snippets.

@MLKrisJohnson
Last active October 6, 2020 17:43
Show Gist options
  • Save MLKrisJohnson/779d0d5424f168ca8f6f6111c7b0c1a2 to your computer and use it in GitHub Desktop.
Save MLKrisJohnson/779d0d5424f168ca8f6f6111c7b0c1a2 to your computer and use it in GitHub Desktop.
Objective-C: Return current date/time in "yyyy-MM-dd HH:mm:ss.SSS Z" format
// Returns current date/time in "yyyy-MM-dd HH:mm:ss.SSSS Z" format using the
// system timezone.
//
// Note that this includes fractional seconds, unlike the system log, so it
// is useful when precise times are desired in log output.
static NSString* timestampString()
{
// Re-use one NSDateFormatter instance for all calls.
// NSDateFormatter is thread-safe for iOS 7+.
static NSDateFormatter* df;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
df = [[NSDateFormatter alloc] init];
df.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSSS z";
});
return [df stringFromDate:[NSDate date]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment