This question already has answers here:
Getting the difference between two Dates (months/days/hours/minutes/seconds) in Swift
(20 answers)
Closed 5 years ago.
currentCalendar.dateComponents(components: Set Calendar.Component, from: Date, to: Date)
I want to calculate the number of days between 2 dates, I can't seem to find a tutorial anyway online for swift4 Xcode9 everything else seems to be about the outdated syntax. so I just want to know what should I put in the "components: Set Calendar.Component" part ?
You should put a Set of the enumeration type Calendar.Component
example:
let s: Set = [Calendar.Component.day]
let c = Calendar(identifier: .gregorian)
let dc = c.dateComponents(s, from: Date.init(timeIntervalSinceNow: -100000), to: Date.init(timeIntervalSinceNow: 0))
let day = dc.day
the day variable is equal to 1
Related
This question already has answers here:
NSDateFormatter and current language in iOS11
(5 answers)
Closed 3 years ago.
When using a DateComponentFormatter like this
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.includesApproximationPhrase = false
formatter.includesTimeRemainingPhrase = false
formatter.allowedUnits = [.hour]
var components = DateComponents()
components.hour = 4
let outputString = formatter.string(from: components)
The output is '4 hours' when my language/region is English/US. When changing the language/region to French/France I had expected the output to be '4 heures`, but it still comes out as '4 hours'. Any suggestions on how to get the DateComponentFormatter to do translations?
Changing the language/region on the phone seems to have no effect.
May be, Your app is not localized to other languages. I followed https://www.raywenderlich.com/250-internationalizing-your-ios-app-getting-started for adding localization in my app.
Per above, the underlying reason is that Apple changed the behavior of the formatters in iOS11 and I hadn't caught that. This question was answered by NSDateFormatter and current language in iOS11
This question already has answers here:
Checking response Time of API in iOS using Swift 3?
(4 answers)
Closed 3 years ago.
I'm trying to find a way to tell how long its taken a function to execute in seconds. Currently I'm doing it this way:
let startDate = Date()
let endDate = Date()
let calendar = Calendar.current
let dateComponents = calendar.compare(startDate, to: endDate, toGranularity: .second)
let seconds = dateComponents.rawValue
print("Seconds: \(seconds)")
but every time I print out the seconds it always reads -1. I've looked into this question: elapsed time but I need the output to be in seconds. Any suggestions?
Try this:
let start = Date.timeIntervalSinceReferenceDate
// do stuff
let end = Date.timeIntervalSinceReferenceDate
let secondsElapsed = end - start
secondsElapsed will be a Double, but it will be in seconds. You can round it or truncate it if you want an Int.
Hope this helps!
This question already has answers here:
NSDate of yesterday
(6 answers)
How to add minutes to current time in swift
(11 answers)
Closed 5 years ago.
I use this code to get the current date
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy.MM.dd"
let result = formatter.string(from: date)
Any idea on how can I get the date in the past, for example a day or a year before the current date?
Try to use this:
Date(timeIntervalSinceNow: -3600); \\One hour
Or you can use Calendar for this
let calendar = Calendar.current
var component = DateComponents()
component.year = -1
calendar.date(byAdding: component, to: Date())
This question already has answers here:
Getting the difference between two Dates (months/days/hours/minutes/seconds) in Swift
(20 answers)
Closed 6 years ago.
I'm playing with swift. I want to show how many years/months/days has been passed since the specific date(NSdate).
If the specific date is 08/01/2016 and current date is 08/11/2016, it will show only "10day(s) has been passed". If the specific date is 08/11/2015, it will show "1year(s) 0 month(s) 0day(s)has been passed"
Do something like this. Use your dates in date1 and date2
let date1 = NSDate()
let date2 = NSDate()
let form = NSDateComponentsFormatter()
form.maximumUnitCount = 2
form.unitsStyle = .Full
form.allowedUnits = [.Year, .Month, .Day]
let s = form.stringFromDate(date1, toDate: date2)
This question already has answers here:
How to add minutes to current time in swift
(11 answers)
Closed 7 years ago.
How can I reduce 5 or 10 minutes from current time and then store it as date format in Swift 2.0 ?
let now = NSDate()
var reducedTime = ???? \\ Here I want 10 minutes reduced from current time.
If current time is 02:20:00, then reducedTime should be 02:10:00
How can I do this in a simplest way ???
You can use calendar method dateByAddingUnit and subtract 10 minutes fro the date.
let now = NSDate()
let reducedTime = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)?.dateByAddingUnit(.Minute, value: -10, toDate: now, options: NSCalendarOptions())