Find Difference in Seconds Between Nsdates as Integer Using Swift

Find difference in seconds between NSDates as integer using Swift

Your attempt to calculate elapsedTime is incorrect. In Swift 3, it would be:

let elapsed = Date().timeIntervalSince(timeAtPress)

Note the () after the Date reference. The Date() instantiates a new date object, and then timeIntervalSince returns the time difference between that and timeAtPress. That will return a floating point value (technically, a TimeInterval).

If you want that as truncated to a Int value, you can just use:

let duration = Int(elapsed)

And, BTW, your definition of the timeAtPress variable doesn't need to instantiate a Date object. I presume you intended:

var timeAtPress: Date!

That defines the variable as a Date variable (an implicitly unwrapped one), but you'd presumably defer the actual instantiation of that variable until pressed is called.


Alternatively, I often use CFAbsoluteTimeGetCurrent(), e.g.,

var start: CFAbsoluteTime!

And when I want to set startTime, I do the following:

start = CFAbsoluteTimeGetCurrent()

And when I want to calculate the number of seconds elapsed, I do the following:

let elapsed = CFAbsoluteTimeGetCurrent() - start

It's worth noting that the CFAbsoluteTimeGetCurrent documentation warns us:

Repeated calls to this function do not guarantee monotonically increasing results. The system time may decrease due to synchronization with external time references or due to an explicit user change of the clock.

This means that if you're unfortunate enough to measure elapsed time when one of these adjustments take place, you can end up with incorrect elapsed time calculation. This is true for NSDate/Date calculations too. It's safest to use a mach_absolute_time based calculation (most easily done with CACurrentMediaTime):

let start = CACurrentMediaTime()

and

let elapsed = CACurrentMediaTime() - start

This uses mach_absolute_time, but avoids some of its complexities outlined in Technical Q&A QA1398.

Remember, though, that CACurrentMediaTime/mach_absolute_time will be reset when the device is rebooted. So, bottom line, if you need accurate elapsed time calculations while an app is running, use CACurrentMediaTime. But if you're going to save this start time in persistent storage which you might recall when the app is restarted at some future date, then you have to use Date or CFAbsoluteTimeGetCurrent, and just live with any inaccuracies that may entail.

Getting the difference between two Dates (months/days/hours/minutes/seconds) in Swift

Xcode 8.3 • Swift 3.1 or later

You can use Calendar to help you create an extension to do your date calculations as follow:

extension Date {
/// Returns the amount of years from another date
func years(from date: Date) -> Int {
return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0
}
/// Returns the amount of months from another date
func months(from date: Date) -> Int {
return Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0
}
/// Returns the amount of weeks from another date
func weeks(from date: Date) -> Int {
return Calendar.current.dateComponents([.weekOfMonth], from: date, to: self).weekOfMonth ?? 0
}
/// Returns the amount of days from another date
func days(from date: Date) -> Int {
return Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0
}
/// Returns the amount of hours from another date
func hours(from date: Date) -> Int {
return Calendar.current.dateComponents([.hour], from: date, to: self).hour ?? 0
}
/// Returns the amount of minutes from another date
func minutes(from date: Date) -> Int {
return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
}
/// Returns the amount of seconds from another date
func seconds(from date: Date) -> Int {
return Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0
}
/// Returns the a custom time interval description from another date
func offset(from date: Date) -> String {
if years(from: date) > 0 { return "\(years(from: date))y" }
if months(from: date) > 0 { return "\(months(from: date))M" }
if weeks(from: date) > 0 { return "\(weeks(from: date))w" }
if days(from: date) > 0 { return "\(days(from: date))d" }
if hours(from: date) > 0 { return "\(hours(from: date))h" }
if minutes(from: date) > 0 { return "\(minutes(from: date))m" }
if seconds(from: date) > 0 { return "\(seconds(from: date))s" }
return ""
}
}

Using Date Components Formatter

let dateComponentsFormatter = DateComponentsFormatter()
dateComponentsFormatter.allowedUnits = [.second, .minute, .hour, .day, .weekOfMonth, .month, .year]
dateComponentsFormatter.maximumUnitCount = 1
dateComponentsFormatter.unitsStyle = .full
dateComponentsFormatter.string(from: Date(), to: Date(timeIntervalSinceNow: 4000000)) // "1 month"


let date1 = DateComponents(calendar: .current, year: 2014, month: 11, day: 28, hour: 5, minute: 9).date!
let date2 = DateComponents(calendar: .current, year: 2015, month: 8, day: 28, hour: 5, minute: 9).date!

let years = date2.years(from: date1) // 0
let months = date2.months(from: date1) // 9
let weeks = date2.weeks(from: date1) // 39
let days = date2.days(from: date1) // 273
let hours = date2.hours(from: date1) // 6,553
let minutes = date2.minutes(from: date1) // 393,180
let seconds = date2.seconds(from: date1) // 23,590,800

let timeOffset = date2.offset(from: date1) // "9M"

let date3 = DateComponents(calendar: .current, year: 2014, month: 11, day: 28, hour: 5, minute: 9).date!
let date4 = DateComponents(calendar: .current, year: 2015, month: 11, day: 28, hour: 5, minute: 9).date!

let timeOffset2 = date4.offset(from: date3) // "1y"

let date5 = DateComponents(calendar: .current, year: 2017, month: 4, day: 28).date!
let now = Date()
let timeOffset3 = now.offset(from: date5) // "1w"

Finding the difference in time between two NSDates Swift

Yes if you look at the second answer to your linked question you can do this:
let interval = laterDate.timeIntervalSinceDate(earlierDate)

iOS: How to calculate milliseconds between two dates

In your stopLoading function you should call:

print(Date().timeIntervalSince(startDate) * 1000)

This way you will see how much time (in ms) has passed since startDate.

How can I calculate the difference between two dates?

NSDate *date1 = [NSDate dateWithString:@"2010-01-01 00:00:00 +0000"];
NSDate *date2 = [NSDate dateWithString:@"2010-02-03 00:00:00 +0000"];

NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];

int numberOfDays = secondsBetween / 86400;

NSLog(@"There are %d days in between the two dates.", numberOfDays);

EDIT:

Remember, NSDate objects represent exact moments of time, they do not have any associated time-zone information. When you convert a string to a date using e.g. an NSDateFormatter, the NSDateFormatter converts the time from the configured timezone. Therefore, the number of seconds between two NSDate objects will always be time-zone-agnostic.

Furthermore, this documentation specifies that Cocoa's implementation of time does not account for leap seconds, so if you require such accuracy, you will need to roll your own implementation.

Difference between 2 dates in seconds ios

since you are not using ARC, when you write

startTime = [NSDate date];

you do not retain startTime, so it is deallocated before -viewWillDisappear is called. Try

startTime = [[NSDate date] retain];

Also, I recommend to use ARC. There should be much less errors with memory management with it, than without it

iOS Swift - Calculate the difference between two times

At login...

    let loginTime = Date()
UserDefaults.standard.set(loginTime, forKey: "loginTime")

Then at logout...

    let loginTime = UserDefaults.standard.object(forKey: "loginTime") as? Date ?? Date()
let loginInterval = -loginTime.timeIntervalSinceNow

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.includesApproximationPhrase = false
formatter.includesTimeRemainingPhrase = false
formatter.allowedUnits = [.hour, .minute]

// Use the configured formatter to generate the string.
let userLoginTimeString = formatter.string(from: loginInterval) ?? ""
print("user was logged in for \(userLoginTimeString)")

Get minutes and hours between two NSDates?

Mac OS X 10.10 Yosemite introduces the smart NSDateComponentsFormatter to display time intervals in components like 18h 56m 4s

This result of this snippet is the formatted time interval from midnight until now.

let calendar = NSCalendar.currentCalendar()
let midnight = calendar.startOfDayForDate(NSDate())
let timeinterval = NSDate().timeIntervalSinceDate(midnight)

let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Abbreviated
formatter.stringFromTimeInterval(timeinterval)


Related Topics



Leave a reply



Submit