开发者

How to display label text with timestamp as "seconds:milliseconds" in iPhone SDK?

开发者 https://www.devze.com 2023-03-15 14:33 出处:网络
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 -

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消