Questions tagged [nstimeinterval]

an Objective-C Foundation Data Type that specifies a time interval

NSTimeInterval is an Foundation Data Type that specifies a time interval. NSTimeInterval values are in seconds.

Resources

Related Tags

364 questions
543
votes
35 answers

Rounding a double value to x number of decimal places in swift

Can anyone tell me how to round a double value to x number of decimal places in Swift? I have: var totalWorkTimeInHours = (totalWorkTime/60/60) With totalWorkTime being an NSTimeInterval (double) in second. totalWorkTimeInHours will give me the…
Leighton
  • 6,559
  • 7
  • 21
  • 28
291
votes
17 answers

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

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) ->…
user3225917
  • 2,991
  • 2
  • 13
  • 17
178
votes
4 answers

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

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 =…
John Gallagher
  • 6,208
  • 6
  • 40
  • 75
107
votes
6 answers

Find difference in seconds between NSDates as integer using Swift

I'm writing a piece of code where I want to time how long a button was held down. To do that I recorded an NSDate() when the button was pressed, and tried using the timeIntervalSinceDate function when the button was released. That seems to work but…
Erik
  • 2,299
  • 4
  • 18
  • 23
105
votes
12 answers

How to convert an NSTimeInterval (seconds) into minutes

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…
Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168
89
votes
15 answers

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

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…
Lydia
  • 2,017
  • 2
  • 18
  • 34
55
votes
9 answers

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

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…
willc2
  • 38,991
  • 25
  • 88
  • 99
46
votes
2 answers

Difference between 2 dates in seconds ios

I have an app where content is displayed to the user. I now want to find out how many seconds a user actually views that content for. So in my header file, I've declared an NSDate *startTime; NSDate *endTime; Then in my viewWillAppear startTime…
Adam Altinkaya
  • 649
  • 1
  • 12
  • 23
42
votes
5 answers

NSTimeInterval Formatting

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?
Baub
  • 5,004
  • 14
  • 56
  • 99
39
votes
6 answers

How to convert NSTimeInterval to int?

How do I convert NSTimeInterval into an Integer value? My TimeInterval holds the value 83.01837. I need to convert it into 83. I have googled but couldn't find any help.
Pradeep Reddy Kypa
  • 3,992
  • 7
  • 57
  • 75
30
votes
2 answers

Convert NSTimeInterval to NSString in iPhone?

How to covert the NSTimeInterval to NSString? I have NSTimeInterval today = [[NSDate date] timeIntervalSince1970]; I have to give "today" as input as NSString.
Warrior
  • 39,156
  • 44
  • 139
  • 214
25
votes
3 answers

Subtracting two NSDate objects

Possible Duplicate: How to Get time difference in iPhone I´m getting date and time from a JSON feed. I need to find the difference between the date I´m getting from the feed and today´s date and time. Any suggestions how I can do this? I know I…
Magnus
  • 1,444
  • 5
  • 22
  • 31
23
votes
4 answers

Subtract minutes from NSDate

I want to subtract some minutes 15 min 10 min etc., And i am having date object with time now i want to subtract minutes.
Ishu
  • 12,797
  • 5
  • 35
  • 51
20
votes
4 answers

Get time difference between two times in swift 3

I have 2 variables where I get 2 times from datePicker and I need to save on a variable the difference between them. let timeFormatter = DateFormatter() timeFormatter.dateFormat = "HHmm" time2 = timeFormatter.date(from:…
ToroLoco
  • 461
  • 1
  • 5
  • 20
18
votes
5 answers

Swift ios date as milliseconds Double or UInt64?

I'm not iOS developer but started learning Swift. I try to convert some logic from Android project to iOS. I have the following method: func addGroupItemSample(sample : WmGroupItemSample){ // some custom class var seconds: NSTimeInterval =…
snaggs
  • 5,543
  • 17
  • 64
  • 127
1
2 3
24 25