- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 NSDateFormatter 来格式化持续时间 ( see here )。
我想使用这样的格式:“3 小时 15 分钟”。
这是我正在使用的格式字符串:@"H' 小时,'m' 分钟'"
。
问题是,对于短于一小时的持续时间,结果类似于“0 小时 35 分钟”。
在这种情况下,我只想显示“35 分钟”。
有没有办法告诉 NSDateFormatter 在没有小时的情况下根本不显示小时,或者我应该只使用 if 语句并手动构造字符串?
编辑:
我很清楚,这很容易通过几行额外的代码手动完成,这就是我过去的做法。我只是想知道格式化程序是否有足够的智慧来自行处理这个问题,因为这似乎是一个常见问题。
最佳答案
从 iOS 8.0 和 Mac OS X 10.10 (Yosemite) 开始,您可以使用 NSDateComponentsFormatter
。因此,让我们将小时和分钟用作 allowedUnits
(如您的示例中所示),将 NSDateComponentsFormatterZeroFormattingBehaviorDropLeading
用作 zeroFormattingBehavior
以减少零小时。示例:
NSTimeInterval intervalWithHours = 11700; // 3:15:00
NSTimeInterval intervalWithZeroHours = 2100; // 0:35:00
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute;
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDropLeading;
NSString *string = [formatter stringFromTimeInterval:intervalWithHours];
NSString *stringWithoutHours = [formatter stringFromTimeInterval:intervalWithZeroHours];
NSLog(@"%@ / %@", string, stringWithoutHours);
// output: 3 hours, 15 minutes / 35 minutes
swift 5 版本:
let intervalWithHours: TimeInterval = 11700
let intervalWithZeroHours: TimeInterval = 2100
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute]
formatter.unitsStyle = .full
formatter.zeroFormattingBehavior = .dropLeading
if let string = formatter.string(from: intervalWithHours), let stringWithoutHours = formatter.string(from: intervalWithZeroHours) {
print("\(string) / \(stringWithoutHours)")
// output: "3 hours, 15 minutes / 35 minutes\n"
}
关于objective-c - 如果时间为零,如何使 NSDateFormatter 不包括时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12239019/
我在 iOS 6 上使用 xcode 4.5(4G182)。NSDateFormatter 在 iOS 6 中显示错误的年份,如何解决? NSDateFormatter *dateFormatter
我在 iOS 6 上使用 xcode 4.5(4G182)。NSDateFormatter 在 iOS 6 中显示错误的年份,如何解决? NSDateFormatter *dateFormatter
我正在尝试创建一个 TableViewCell,就像 iPhone 上的 Notes 应用程序中的那样。标题在左侧,时间在右侧,紧挨着披露者指示符。 看起来像那样的 tableView。我知道我必须使
我有一个用于“从”和“到”字段的日期选择器,我想要减法的结果。例如:toValue-fromValue,结果以小时为单位。如何做到这一点? 最佳答案 可以使用timeIntervalSinceDate
我简单地尝试重新格式化日期: NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; NSDate *date = [[NSDat
在 Instrument 的帮助下,我发现以下代码段存在内存泄漏。仪器显示 NSDateFormatter 此处泄漏内存。 - (NSDate*) dateSelected{ if(selectedD
当用户未使用公历作为 iPhone 的默认日历时,我在使用 NSDateFormatter 时遇到问题。 NSString *testString = @"2011-01-14"; NSDateFor
以下日期/时间的 NSDateFormatter 字符串是什么:02/22/2011 10:43:00 AM 基本上我想要的是读取字符串02/22/2011 19:00:23 PM,将其转换为NSDa
我正在尝试创建一个格式化程序,将显示的日期格式转换为 NSDate 对象: NSString *dateStr = @"2010-06-21T19:00:00-05:00"; NSDateFormat
我有一个带有 NSDateFormatter 的 NSTextField。格式化程序接受“mm/dd/yy”。 可以自动补全日期吗?因此,用户可以输入“mm”,格式化程序将完成当前月份和年份。 最佳答
我正在从表示日期的文本中提取字符串。它们看起来像这样: Monday August 16, 2010 05:28 AM EST 我正在尝试使用 NSDateFormatter 来解析它们。我已将其格式
我有一个应用于 NSTextFieldCell 的 10.4 样式日期格式化程序。我调用 setTwoDigitStartDate: (尝试使用纪元日期和“引用”日期),但是当我输入带有两位数年份的日
在打开 doesRelativeDateFormatting 的情况下使用 NSDateFormatter 时,所有相对格式似乎都相对于当前系统时间。我想使用相对格式设置日期,例如“今天”或“明天”,
我想从日期获取 NSString,日期格式如下: MMEEEY ww 问题出在“ww”,即一年中的第几周。我必须将此格式与从后台获取的另一种格式进行比较。但后台的第一个工作日是星期一,而 iOS 的第
我的日期格式化程序返回nil表示一个特定日期:“1998年3月20日”。 + (NSDate*)dateFromString:(NSString*)date { NSDateFormatter
我知道这篇文章可能一式三份,但是到目前为止,关于现有线程的所有答案都没有帮助我。 我试图从字符串中获取NSDate值,但结果一直是nil。这是我的代码 NSString * dateString =
Crashlytics服务报告一些崩溃(大约30个崩溃/ 18个用户进行1000个 session ) 这是我的代码: var brutDate: String = "" brutDate () (
我正在尝试解析这个日期时间戳 开始串: Wed, 11 Sep 2013 08:51:41 EEST 这个问题只有 EEST , 我试过 z 或 zzz 或 V,什么也没发生。日期格式化程序始终为 N
这个问题在这里已经有了答案: NSDate Format outputting wrong date (5 个回答) 8年前关闭。 我正在使用下面的代码,但它给了我提前一天 date 。 示例 :将输
使用设备上用户的currentLocale,我想要一种与NSDateFormatterFullStyle生成的日期格式完全相同的日期格式减去年份: [dateFormatter setDateStyl
我是一名优秀的程序员,十分优秀!