nsdatecomponentsformatter

DateComponentsFormatter drop zero hours but not zero minutes

自作多情 提交于 2019-12-12 12:44:04
问题 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 formatter.zeroFormattingBehavior = .dropLeading But, this causes the minutes to be dropped as well if both hours and minutes are 0. Currently, my solution is as follows: if timeElapsed >= 3600 { formatter.allowedUnits = [.second, .minute, .hour] } else { formatter.allowedUnits =

Swift DateComponentsFormatter drop leading zeroes but keep at least one digit in Minutes place

℡╲_俬逩灬. 提交于 2019-12-11 04:47:52
问题 I am using the following DateComponentsFormatter in Swift: let formatter = DateComponentsFormatter() formatter.unitsStyle = .positional formatter.allowedUnits = [.hour, .minute, .second] formatter.zeroFormattingBehavior = [.default] This produces results like 2:41 (for 2 minutes and 41 seconds) and 08 (for 8 seconds). I would, however, like to keep at least one digit in the Minutes place, returning 0:08 instead of 08. Is this possible with the DateComponentsFormatter? I would prefer a

Getting screwy results from NSDateComponentsFormatter

杀马特。学长 韩版系。学妹 提交于 2019-12-08 11:37:21
问题 I recently learned about the new-to-iOS8 class NSDateComponentsFormatter. It lets you generate time strings for time-spans rather than dates. Pretty cool. I decided to tinker with it, and wrote some test code that creates various time-spans and logs the output. Here's my code (English and French output) NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponentsFormatter* formatter = [[NSDateComponentsFormatter alloc] init];

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

丶灬走出姿态 提交于 2019-12-06 03:05:29
问题 Question Why is string nil? let formatter = NSDateComponentsFormatter() let referenceDate = NSDate(timeIntervalSinceReferenceDate: 0) let intervalDate = NSDate(timeInterval: 3628810, sinceDate: referenceDate) let string = formatter.stringFromDate(referenceDate, toDate: intervalDate) I'm expecting a string like "6w 10s" to be returned. (6 weeks is 3,628,800 seconds.) Attempted Troubleshooting To troubleshoot, I tried setting allowedUnits : formatter.allowedUnits = .YearCalendarUnit |

What am I doing wrong with allowsFractionalUnits on NSDateComponentsFormatter?

雨燕双飞 提交于 2019-12-04 08:51:32
问题 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 NSDateComponentsFormatter.allowsFractionalUnits, but I can't get the formatter to give me a decimal value. Can anyone help me spot my error or tell me in what way I misunderstand this? From Apple docs about allowsFractionalUnits property: Fractional

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

天涯浪子 提交于 2019-12-04 06:12:15
Question Why is string nil? let formatter = NSDateComponentsFormatter() let referenceDate = NSDate(timeIntervalSinceReferenceDate: 0) let intervalDate = NSDate(timeInterval: 3628810, sinceDate: referenceDate) let string = formatter.stringFromDate(referenceDate, toDate: intervalDate) I'm expecting a string like "6w 10s" to be returned. (6 weeks is 3,628,800 seconds.) Attempted Troubleshooting To troubleshoot, I tried setting allowedUnits : formatter.allowedUnits = .YearCalendarUnit | .MonthCalendarUnit | .WeekCalendarUnit | .DayCalendarUnit | .HourCalendarUnit | .MinuteCalendarUnit |

What am I doing wrong with allowsFractionalUnits on NSDateComponentsFormatter?

北战南征 提交于 2019-12-03 01:08:25
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 NSDateComponentsFormatter.allowsFractionalUnits , but I can't get the formatter to give me a decimal value. Can anyone help me spot my error or tell me in what way I misunderstand this? From Apple docs about allowsFractionalUnits property: Fractional units may be used when a value cannot be exactly represented using the available units. For example, if

What am I doing wrong with NSDateComponentsFormatter?

此生再无相见时 提交于 2019-12-02 13:57:11
问题 I recently found out about the new-to-iOS8 class NSDateComponentsFormatter, which lets you format time intervals rather than dates. Cool. I've written code to do this more times than I care to think about. So I decided to try and use it in a playground, but I can't get it to work. Here's my (Swift) code: var componentsFormatter = NSDateComponentsFormatter() componentsFormatter.allowedUnits = .CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond componentsFormatter.unitsStyle =

What am I doing wrong with NSDateComponentsFormatter?

对着背影说爱祢 提交于 2019-12-02 07:24:15
I recently found out about the new-to-iOS8 class NSDateComponentsFormatter, which lets you format time intervals rather than dates. Cool. I've written code to do this more times than I care to think about. So I decided to try and use it in a playground, but I can't get it to work. Here's my (Swift) code: var componentsFormatter = NSDateComponentsFormatter() componentsFormatter.allowedUnits = .CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond componentsFormatter.unitsStyle = .Positional let interval: NSTimeInterval = 345.7 let intervalstring = componentsFormatter