Altaf Bangash
Altaf Bangash

Reputation: 71

getting wrong string value from NSDate

I'm using AVPlayer. And I want to get current time of player to show for user. The problem is the string getting wrong value from NSDate you can see in images

enter image description here

This is date value

enter image description here

And this string value getting from date

please help to solve. this is my code

NSDate* d = [NSDate dateWithTimeIntervalSince1970:CMTimeGetSeconds(mPlayer.currentTime)];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [dateFormatter setDateFormat:@"mm:ss"];

    NSString* result = [dateFormatter stringFromDate:d];
    avPlayerLabel.text = result;

Upvotes: 1

Views: 73

Answers (1)

Droppy
Droppy

Reputation: 9731

You don't even need an NSDateFormatter:

Float64 time = CMTimeGetSeconds(mPlayer.currentTime) + 0.5;
unsigned minutes = (unsigned)time / 60;
unsigned seconds = (unsigned)time % 60;
avPlayerLabel.text = [NSString stringWithFormat:@"%02u:%02u", minutes, seconds];

Upvotes: 2

Related Questions