How to display label text with timestamp as "seconds:milliseconds" in iPhone SDK?
How can I have a timestamp as "ss:mm" where mm = milliseconds?
Please let me kn开发者_运维百科ow.
Getting milliseconds is actually pretty easy. I created a category a while back to do this for me - all you need is a NSTimeInterval
.
- (NSNumber *)getMilliseconds
{
return [NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970] * 1000];
}
So if you want a timestamp with seconds and milliseconds, do this:
NSDate *today = [NSDate dateWithTimeIntervalSinceNow:0];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"ss"];
NSString *dateString = [dateFormat stringFromDate:today];
NSString*mystamp = [NSString stringWithFormat:@"%@:%i",dateString,[self getMilliseconds]];
NSLog(@"Timestamp with Milliseconds: %@", mystamp);
If course you will have to edit my category if you want a custom date to calculate milliseconds from.
Hope it helps!
精彩评论