Questions tagged [nsdatecomponentsformatter]

DateComponentsFormatter is an Apple Foundation type (DateComponentsFormatter in Swift, NSDateComponentsFormatter in Objective-C), that takes quantities of time and formats them as a localized, user-readable strings.

DateComponentsFormatter (known as simply DateComponents in , but as NSDateComponentsFormatter in ) is an Apple type, that takes quantities of time and formats them as a localized, user-readable strings. It is used to display a length of time in the UI.

The date components formatter can be used with either a TimeInterval (a Double representation of the number of seconds) or a DateComponents (a structure that represents a length of time in “units”, such as year, month, day, hour, and minute).

See also:

48 questions
35
votes
3 answers

What am I doing wrong with allowsFractionalUnits on NSDateComponentsFormatter?

Basically what I want is to get the value of a time interval represented in hours only, without rounding it to full hours (using NSDateComponentsFormatter to get it properly formatted and localized). I don't know if I misunderstand the use of…
osanoj
  • 1,035
  • 10
  • 9
16
votes
1 answer

Can DateComponentsFormatter format fractional seconds?

I want to print, e.g., the value 1.5 as "1.5 sec", like Safari's timeline does, and I'm trying to use DateComponentsFormatter for it. Unfortunately, its .allowedUnits only goes down to .second (even though the enum has .nanosecond). I tried setting…
12
votes
5 answers

DateComponentsFormatter returns wrong number of unit count

I've faced issue, when DateComponentsFormatter returns unexpected number of units. Does anyone faced same issue? import Foundation let formatter = DateComponentsFormatter() formatter.unitsStyle = .full; formatter.maximumUnitCount = 1; let date =…
Timur Bernikovich
  • 5,660
  • 4
  • 45
  • 58
11
votes
4 answers

NSDateComponentsFormatter's stringFromDate(_, toDate:) returns nil

Question Why is string nil? let formatter = NSDateComponentsFormatter() let referenceDate = NSDate(timeIntervalSinceReferenceDate: 0) let intervalDate = NSDate(timeInterval: 3628810, sinceDate: referenceDate) let string =…
10
votes
4 answers

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.zeroFormattingBehavior = [.default] This produces…
8
votes
0 answers

DateComponentsFormatter drop zero hours but not zero minutes

I'm trying to use a DateComponentsFormatter to format a number of seconds into a time string HH:MM:SS. I always want to show the minutes and seconds fields but I don't want to show hours if hours is 0. I tried setting…
4
votes
0 answers

DateComponentsFormatter not honoring formattingContext

When I use a standard NumberFormatter, it honors the formattingContext of .beginningOfSentence: let formatter = NumberFormatter() formatter.numberStyle = .spellOut formatter.formattingContext = .beginningOfSentence let string = formatter.string(for:…
Rob
  • 415,655
  • 72
  • 787
  • 1,044
4
votes
2 answers

How to set DateComponentFormatter's local other than current?

Situation I have a function that uses DateComponentFormatter's function fun string(from: Date, to: Date) to return a formatted string based on the time difference between two dates and it works perfectly. However I want to return this formatted…
standousset
  • 1,092
  • 1
  • 10
  • 25
4
votes
0 answers

DateComponentsFormatter string(from:) returned nil in crash report

I have a crash log that indicates that my app crashed on a line with a forced unwrap of the optional. func formattedTimeString(from seconds: TimeInterval) -> String { let formatter = DateComponentsFormatter() formatter.allowedUnits = [.hour,…
shim
  • 9,289
  • 12
  • 69
  • 108
3
votes
1 answer

Have I found a bug in DateComponentsFormatter?

In answering this question on how to create a "xxx ago" string based on comparing two dates, I wrote out a complete solution to the problem. It involves using a DateComponentsFormatter with maximumUnitCount set to 1 and…
Duncan C
  • 128,072
  • 22
  • 173
  • 272
3
votes
0 answers

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: DateComponentsFormatter.UnitsStyle = .positional) ->…
3
votes
3 answers

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. public func captureStartTime() { …
3
votes
1 answer

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 something here that I'm not aware of. Here are 3…
3
votes
1 answer

DateComponentsFormatter - showing only hour described as minutes - bug?

I'm using DateComponentsFormatter to achieve something like that: "1 hour, 30 min". This is my code for that: let formatter = DateComponentsFormatter() formatter.unitsStyle = .short formatter.allowedUnits = [.hour, .minute] return…
Michał Kwiecień
  • 2,734
  • 1
  • 20
  • 23
3
votes
1 answer

NSDateComponentsFormatter for not correctly translating German?

NSDateComponentsFormatter *formatter; formatter = [[NSDateComponentsFormatter alloc] init]; formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleShort; formatter.allowedUnits = NSCalendarUnitDay; NSString *string = [formatter…
anna
  • 2,723
  • 4
  • 28
  • 37
1
2 3 4