48 questions
4 votes
0 answers
181 views
DateComponentsFormatter not honoring formattingContext
When I use a standard NumberFormatter, it honors the formattingContext of .beginningOfSentence: let formatter = NumberFormatter() formatter.numberStyle = .spellOut formatter.formattingContext = ....
8 votes
1 answer
6k views
How to format a time interval with the new Foundation formatted method?
I'm trying to use the new Foundation formatters. To format a Date, I can do something like this: Date.now.formatted(.dateTime.hour().minute().second()) // 5:03:17 PM However, I'm trying to use this ...
0 votes
1 answer
235 views
DateComponentsFormatter - same dates, different results
Why is dateRemainingText2 giving different result from dateRemainingText? Obviously dateRemainingText2 is wrong. Here's my code: import Foundation let startDate = Calendar.current.date(from: ...
2 votes
1 answer
3k views
How to format DateComponents as a partial date string?
I have a DateComponents object that contains a month and a year and I'd like to have a formatted string from it in the style 'March 2021'. I've tried DateComponentsFormatter but none of the properties ...
0 votes
0 answers
293 views
Swift - multiple unitsStyles in DateComponentsFormatter?
I'm trying to find a way to use Swift's DateComponentsFormatter with multiple unitsStyle properties, depending on the actual component. For example, I'd like to use .abbreviated for the day property ...
0 votes
0 answers
325 views
How to modify formatted string with 'positional' option by using swift's DateComponentsFormatter
I want to get a relatively formatted string from TimeInterval. So I have tried following code to get custom formatted string: let ft = DateComponentsFormatter() ft.unitsStyle = .positional ft....
3 votes
1 answer
384 views
Have I found a bug in DateComponentsFormatter?
In answering this question on how to create a "xxx <largest_time_units> ago" string based on comparing two dates, I wrote out a complete solution to the problem. It involves using a ...
0 votes
2 answers
2k views
Can I use a DateComponentsFormatter to render the time of day?
I would like to store a time of day in a simple way. For the purposes of my app, I only care about a specific time of day, like 2:30pm or 1:10am. Using a DateComponentsFormatter I can get really close....
5 votes
0 answers
445 views
Problem with swifts DateComponentsFormatter.zeroFormattingBehavior = .pad when displaying negative seconds
I'm working on a timer in swift and came across a weird behavior for the DateComponentsFormatter when trying to format it to .zeroFormattingBehavior = .pad func timerString(duration:Int, style: ...
0 votes
1 answer
217 views
reset date to next hour after top of hour is reached
I Want My swift code to count down to the nearest top of the hour. So if the time is 146 the user code should count down 14 minutes. Right now My code below counts down to a spefic day and time. I ...
0 votes
2 answers
195 views
DateComponentFormatter Translation [duplicate]
When using a DateComponentFormatter like this let formatter = DateComponentsFormatter() formatter.unitsStyle = .full formatter.includesApproximationPhrase = false formatter....
1 vote
0 answers
170 views
How to show the Netherlands time in hours (uur) in Dutch?
Show time for multiple countries with native time format. Trying to Display time of Netherland eg: 22.37 uur With Locale: nl_NL For Date: 2019-09-17 17:07:00 +0000 I have tried the code below: ...
6 votes
4 answers
6k views
Find difference in two dates in iOS
I am not sure what I am doing wrong, I need to find difference between two dates and extract seconds from it, below is my code. I am not getting correct seconds. There is difference of seconds. ...
3 votes
1 answer
987 views
DateComponentsFormatter and .nanosecond
So, I'm using a DateComponentsFormatter in a situation where I need to format only the nanoseconds of a given TimeInterval. The problem is that it just does not work as intended, or I'm confusing ...
11 votes
4 answers
5k views
Swift DateComponentsFormatter drop leading zeroes but keep at least one digit in Minutes place
I am using the following DateComponentsFormatter in Swift: let formatter = DateComponentsFormatter() formatter.unitsStyle = .positional formatter.allowedUnits = [.hour, .minute, .second] formatter....