nstimeinterval

how to pause and resume NSTimer in iphone

China☆狼群 提交于 2019-11-27 23:38:49
hello I am developing small gameApp. I need to pause the timer,when user goes to another view [say settings view]. when user comes back to that view , I need to resume the timer. can anybody solve this issue ... Thanks in Advance... You can't pause a timer. However, when the user goes to the settings view, you can save the fireDate of the timer and also the current date. After this you invalidate the timer and let the user do his/her stuff. Once he/she switches back to the game, you create a new timer object and set the fire date to the old fire date plus the time the user was in the menu

NSTimeInterval Formatting

泄露秘密 提交于 2019-11-27 12:02:21
I want to take my NSTimeInterval and format it into a string as 00:00:00 (hours, minutes, seconds). What is the best way to do this? Michael Frederick NSTimeInterval interval = ...; NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval]; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"HH:mm:ss"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; NSString *formattedDate = [dateFormatter stringFromDate:date]; NSLog(@"hh:mm:ss %@", formattedDate); Johan Kool Since iOS 8.0 there is now NSDateComponentsFormatter

conversion from NSTimeInterval to hour,minutes,seconds,milliseconds in swift

大城市里の小女人 提交于 2019-11-27 09:57:26
问题 My code is here: func stringFromTimeInterval(interval:NSTimeInterval) -> NSString { var ti = NSInteger(interval) var ms = ti * 1000 var seconds = ti % 60 var minutes = (ti / 60) % 60 var hours = (ti / 3600) return NSString(format: "%0.2d:%0.2d:%0.2d",hours,minutes,seconds,ms) } in output the milliseconds give wrong result.Please give an idea how to find milliseconds correctly. 回答1: Swift supports remainder calculations on floating-point numbers, so we can use % 1 . var ms = Int((interval % 1)

Compare current time with two times-of-day strings [closed]

可紊 提交于 2019-11-27 09:54:00
How can i get time like "11:30" formate so that i want to compare it with the following: strOpenTime = @"10:00"; strCloseTime = @"2:00"; so how can i get current time like as above open/close time format and i want if the current time is inside the interval open/close time? Thanks in advance..!! First you have to convert the strings "10:00", "2:00" to a date from the current day . This can be done e.g. with the following method (error checking omitted for brevity): - (NSDate *)todaysDateFromString:(NSString *)time { // Split hour/minute into separate strings: NSArray *array = [time

How to Get time difference in iPhone

痞子三分冷 提交于 2019-11-27 09:30:42
I have 2 arrays with time values in it. They are in the following format. mm:ss:hundreds of a sec. I want to get the difference between the two [lastObjects] in the arrays. NSDate is not working because the last value is in hundredsth of a sec. A question. If the second date is larger than the first will it give me a negative number like -01:10:00 ? Your problem has two parts, parsing the time and getting the difference: NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease

NSTimeInterval to NSDate

大憨熊 提交于 2019-11-27 08:53:51
How can I convert a NSTimeInterval to NSDate ? Think of it like a stopwatch. I want the initial date to be 00:00:00, and I have a NSTimeInterval of X seconds. I need to do it like this because the NSTimeInterval needs to be converted to an int by using lround to round up, then converted to a NSDate to use the NSDateFormatter to throw it into a string. An NSTimeInterval , as its name, um, implies, doesn't represent the same thing as an NSDate . An NSDate is a moment in time. A time interval is a stretch of time. To get a point from an interval, you have to have another point. Your question is

How to parse an ISO-8601 duration in Objective C?

半世苍凉 提交于 2019-11-27 04:06:45
I'm looking for an easy way to parse a string that contains an ISO-8601 duration in Objective C. The result should be something usable like a NSTimeInterval . An example of an ISO-8601 duration: P1DT13H24M17S , which means 1 day, 13 hours, 24 minutes and 17 seconds. If you know exactly which fields you'll be getting, you can use one invocation of sscanf() : const char *stringToParse = ...; int days, hours, minutes, seconds; NSTimeInterval interval; if(sscanf(stringToParse, "P%dDT%dH%dM%sS", &days, &hours, &minutes, &seconds) == 4) interval = ((days * 24 + hours) * 60 + minutes) * 60 + seconds;

What's the optimum way of storing an NSDate in NSUserDefaults?

我们两清 提交于 2019-11-27 00:03:55
There's two ways of storing an NSDate in NSUserDefaults that I've come across. Option 1 - setObject:forKey: // Set NSDate *myDate = [NSDate date]; [[NSUserDefaults standardUserDefaults] setObject:myDate forKey:@"myDateKey"]; // Get NSDate *myDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"myDateKey"]; Option 2 - timeIntervalSince1970 // Set NSDate *myDate = [NSDate date]; NSTimeInterval myDateTimeInterval = [myDate timeIntervalSince1970]; [[NSUserDefaults standardUserDefaults] setFloat:myDateTimeInterval forKey:@"myDateKey"]; // Get NSTimeInterval myDateTimeInterval = [

how to pause and resume NSTimer in iphone

徘徊边缘 提交于 2019-11-26 21:33:15
问题 hello I am developing small gameApp. I need to pause the timer,when user goes to another view [say settings view]. when user comes back to that view , I need to resume the timer. can anybody solve this issue ... Thanks in Advance... 回答1: You can't pause a timer. However, when the user goes to the settings view, you can save the fireDate of the timer and also the current date. After this you invalidate the timer and let the user do his/her stuff. Once he/she switches back to the game, you

How to convert an NSTimeInterval (seconds) into minutes

可紊 提交于 2019-11-26 21:20:38
I've got an amount of seconds that passed from a certain event. It's stored in a NSTimeInterval data type. I want to convert it into minutes and seconds . For example I have: "326.4" seconds and I want to convert it into the following string: "5:26". What is the best way to achieve this goal? Thanks. pseudo-code: minutes = floor(326.4/60) seconds = round(326.4 - minutes * 60) Brief Description The answer from Brian Ramsay is more convenient if you only want to convert to minutes. If you want Cocoa API do it for you and convert your NSTimeInterval not only to minutes but also to days, months,