nstimeinterval

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

怎甘沉沦 提交于 2019-11-26 17:27:40
问题 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

NSTimeInterval Formatting

纵饮孤独 提交于 2019-11-26 15:52:42
问题 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? 回答1: 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

How do I break down an NSTimeInterval into year, months, days, hours, minutes and seconds on iPhone?

﹥>﹥吖頭↗ 提交于 2019-11-26 15:07:21
I have a time interval that spans years and I want all the time components from year down to seconds. My first thought is to integer divide the time interval by seconds in a year, subtract that from a running total of seconds, divide that by seconds in a month, subtract that from the running total and so on. That just seems convoluted and I've read that whenever you are doing something that looks convoluted, there is probably a built-in method. Is there? I integrated Alex's 2nd method into my code. It's in a method called by a UIDatePicker in my interface. NSDate *now = [NSDate date]; NSDate

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

天大地大妈咪最大 提交于 2019-11-26 14:56:23
问题 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..!! 回答1: 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 *

How to Get time difference in iPhone

余生长醉 提交于 2019-11-26 14:46:39
问题 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 ? 回答1: Your problem has two parts, parsing the time and getting the difference: NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init]

NSTimeInterval to NSDate

空扰寡人 提交于 2019-11-26 14:24:12
问题 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. 回答1: 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

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

安稳与你 提交于 2019-11-26 12:42:49
问题 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. 回答1: 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

How to convert an NSTimeInterval (seconds) into minutes

十年热恋 提交于 2019-11-26 09:05:36
问题 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. 回答1: pseudo-code: minutes = floor(326.4/60) seconds = round(326.4 - minutes * 60) 回答2: Brief Description The answer from Brian Ramsay is more convenient if you only want to convert to

How do I break down an NSTimeInterval into year, months, days, hours, minutes and seconds on iPhone?

老子叫甜甜 提交于 2019-11-26 04:09:58
问题 I have a time interval that spans years and I want all the time components from year down to seconds. My first thought is to integer divide the time interval by seconds in a year, subtract that from a running total of seconds, divide that by seconds in a month, subtract that from the running total and so on. That just seems convoluted and I\'ve read that whenever you are doing something that looks convoluted, there is probably a built-in method. Is there? I integrated Alex\'s 2nd method into

How can I use Timer (formerly NSTimer) in Swift?

拈花ヽ惹草 提交于 2019-11-26 00:32:11
问题 I tried var timer = NSTimer() timer(timeInterval: 0.01, target: self, selector: update, userInfo: nil, repeats: false) But, I got an error saying \'(timeInterval: $T1, target: ViewController, selector: () -> (), userInfo: NilType, repeats: Bool) -> $T6\' is not identical to \'NSTimer\' 回答1: This will work: override func viewDidLoad() { super.viewDidLoad() // Swift block syntax (iOS 10+) let timer = Timer(timeInterval: 0.4, repeats: true) { _ in print("Done!") } // Swift >=3 selector syntax