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
This is date value
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;
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];
Related
My Json is
{
"MinPerAppointment": "30",
"OpeningTime": "12:05"
}
Now, I want to Add 30 to 12:05, which Should be 12:35, I could not get How to format it, SO, I tried it with NSdate
NSString *str_MinPerAppointment = timeDetailData.minPerAppointment;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm"];
NSDate *myDate = [dateFormatter dateFromString:str_MinPerAppointment];
NSString *dateInString = [dateFormatter stringFromDate:myDate];
NSLog(#"New AppointMentValue :%#",dateInString);
I am getting New AppointMentValue as nil.
I don't think you need to use NSDate formatting. Simply convert HH:MM to minutes (or seconds, if you deal with them), do your math and then convert back.
Something like:
NSString *time = #"12:30";
int addMinutes = 30;
int hh, mm;
if (sscanf([time UTF8String], "%d:%d", &hh, &mm) == 2) {
int minutes = (hh * 60) + mm;
minutes += addMinutes;
hh = minutes / 60;
mm = minutes - (hh * 60);
hh %= 24; // day roll-over
NSString *newTime = [NSString stringWithFormat:#"%02:%02d", hh, mm];
} else {
NSLog(#"Invalid time value: %#", time);
}
If you want to use NSDateFormatter/NSDate try this-
NSString *str_MinPerAppointment = #"30";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm"];
NSDate *myDate = [dateFormatter dateFromString:#"12:05"] ;
NSString *dateInString = [dateFormatter stringFromDate:[myDate dateByAddingTimeInterval:60*[str_MinPerAppointment integerValue]]];
NSLog(#"New AppointMentValue :%#",dateInString);
I'm trying to get timeIntervalSinceDate to send a log in secs of how much time has elapsed since a previous date but it isn't working. I set it for 1 minute in the future each time I test it but I not get logs that correspond like the latest log was this: 2015-09-22 16:30:04.469 Wakey Wakey[49785:7077539] Seconds nan 2015-09-22 16:30:04.470. This is the code:
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date ];
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd:MM:YYYY:ss:mm:hh"];
NSDate *dateTimeSeconds = [[NSDate alloc] init];
dateTimeSeconds = [dateFormatter1 dateFromString:dateTimeString];
NSTimeInterval secs = [currentTime timeIntervalSinceDate:dateTimeSeconds];
NSLog(#"Seconds %g", secs);
What should I do to fix it?
Why bother with NSDateFormatter. Try
NSTimeInterval seconds = [[NSDate date] timeIntervalSinceDate:dateTimePicker.date];
NSLog(#"seconds %.f", seconds);
Also use %.f instead of %g on the NSLog
Try with this
NSDate* date1 = someDate;
NSDate* date2 = someOtherDate;
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
Please try with below date format
kTimeStampFormat #"dd-MM-yyyy'T'HH:mm:ss.SSS'Z'"
Use UTC format for consistency
Time profiler say that my code to expressive in memory and I see lags while scrolling tableView.
How can I replace this code?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm:ss"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[_duration doubleValue]];
return [dateFormatter stringFromDate:date];
_duration have value 123
I need string from _duration like 2:03 sec
Two options:
1) Create the date formatter once and reuse it.
2) Don't use a date formatter. There's no need. It's trivial to convert your duration to minutes and seconds.
int duration = [_duration intValue];
int mins = duration / 60;
int secs = duration % 60;
return [NSString stringWithFormat:#"%02d:%02d", mins, secs];
After adding a comment i need to show comment timing like year ago,day ago,minutes ago and second ago on label but it returns -4425,-33434 seconds ago "random numbers".
Here is my code shown below am using in my app.
NSString *inDateStr = [NSString stringWithFormat:#"%#",[[[dict objectForKey:#"d"] objectAtIndex:indexPath.row-1] objectForKey:#"created"]];
NSString *s = #"yyyy-MM-dd HH:mm:ss";
// about input date(GMT)
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init];
inDateFormatter.dateFormat = s;
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT-7.00"];
NSDate *inDate = [inDateFormatter dateFromString:inDateStr];
// about output date(IST)
NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init];
outDateFormatter.timeZone = [NSTimeZone localTimeZone];
outDateFormatter.dateFormat = s;
NSString *outDateStr = [outDateFormatter stringFromDate:inDate];
// final output
NSLog(#"[in]%# -> [out]%#", inDateStr, outDateStr);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dateStartingString = [[NSDate alloc] init];
NSString *datestartString = outDateStr;
dateStartingString = [dateFormatter dateFromString:datestartString];
NSTimeInterval timeDifference = [[NSDate date] timeIntervalSinceDate:dateStartingString];
double minutes = timeDifference / 60;
double hours = minutes / 60;
double seconds = timeDifference;
double days = minutes / 1440;
UILabel * dateLbl=[[UILabel alloc] initWithFrame:CGRectMake(155, [[CountArr objectAtIndex:indexPath.row] floatValue]-1, 80, 20)];
if(seconds>=86400)
dateLbl.text=[NSString stringWithFormat:#"%.0f days ", days];
else if(seconds>=3600 && seconds<86400 )
dateLbl.text=[NSString stringWithFormat:#"%.0f hours ", hours];
else if (seconds>=60 && seconds<3600 )
dateLbl.text=[NSString stringWithFormat:#"%.0f minutes ", minutes];
else
dateLbl.text=[NSString stringWithFormat:#"%.0f seconds ", seconds];
dateLbl.textColor=[UIColor lightGrayColor];
dateLbl.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:dateLbl];
I believe NSDateFormatter only takes a string and returns a NSDate object. Setting the timezone is meant for setting the property of NSDate, but doesn't actually convert the time provided. So in the end if you put in 5:30 GMT, you will get 5:30 IST.
You would instead want to use NSCalender to do timezone conversions.
You can use this method to get difference in date.
Make sure that date formate is correct. If you are getting negative value just check date format.
-(NSDateComponents *)getDateDifference:(NSString *)date
{
NSDate *dateA=[NSDate date]; //Current date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateB = [dateFormatter dateFromString:date];//Convert nsstring to nsdate
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; //Gregorian allocation
NSDateComponents *components = [calendar components:NSDayCalendarUnit|NSHourCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit
fromDate:dateA
toDate:dateB
options:0]; //Get day and hour
return components;
}
// Call method like
NSDateComponents *difference = [self getDateDifference:marriageDate];
NSLog(#"diff year = %f",difference.year);
i am using the following code to convert a value of 18900 into hh:mm:ss
unfortunately the value returned doesn't seem right as it gives me 06:15:00
can anyone please advise why its going wrong
thanks
-(NSString*)setHours:(double)result
{
NSLog(#"result %0.2f",result);
NSDate *date = [NSDate dateWithTimeIntervalSince1970:result];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm:ss"];
NSString *localizedString = [formatter stringFromDate:date];
[formatter release];
return localizedString;
}
Since you have a value that is the number of seconds since midnight, you don't need a date formatter. Try this:
-(NSString*)setHours:(double)result {
int secs = result;
int h = secs / 3600;
int m = secs / 60 % 60;
int s = secs % 60;
NSString *text = [NSString stringWithFormat:#"%02d:%02d:%02d", h, m, s];
}
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
First of all, you have to convert miliseconds in to seconds by dividing miliseconds by 1000.
NSTimeInterval result= 18900/1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:result];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSString *formattedDate=[formatter stringFromDate:date];
NSLog(#"Formatted Date : %#",formattedDate);
It's giving you a result for your current time zone, and the time is specified in UTC. So you're an hour after GMT I suppose.
Check out this question: NSDate dateWithTimeIntervalSince1970 NOT returning GMT/UTC time