简体   繁体   English

使用NSDateIntervalFormatter将持续时间格式化为ISO8601周期

[英]Format Duration as ISO8601 Period with NSDateIntervalFormatter

I am trying to convert the duration between two dates to the format "'P'yyyy'Y'M'M'd'DT'H'H'm'M's.S'S'" . 我正在尝试将两个日期之间的持续时间转换为格式"'P'yyyy'Y'M'M'd'DT'H'H'm'M's.S'S'" An example of my expected output is: P0Y0M0DT0H0M0.002S (this is port of an Android project that uses DurationFormatUtils ). 我的预期输出的示例是: P0Y0M0DT0H0M0.002S (这是使用DurationFormatUtils的Android项目的端口)。

My test case: 我的测试用例:

NSDateIntervalFormatter *durationFormatter = [NSDateIntervalFormatter durationFormatter];
NSDate *date = [NSDate date];
NSDate *end = [date dateByAddingTimeInterval:25];
NSString *duration = [durationFormatter stringFromDate:date toDate:end];
XCTAssertTrue( [duration hasPrefix:@"P"], @"Actual String: %@", duration );

My formatter is defined as: 我的格式化程序定义为:

+ (NSDateIntervalFormatter*) durationFormatter {
    static NSDateIntervalFormatter *formatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatter = [[NSDateIntervalFormatter alloc] init];
        formatter.dateTemplate = @"'P'yyyy'Y'M'M'd'DT'H'H'm'M's.S'S'";
    });

    return formatter;
}

When I run this, duration is 3/5/2015, 16:46:20.3 . 当我运行此代码时, duration为3/5/ 2015,16:46:20.3 If I set the locale on the formatter to nil and set the date and time format to NSDateIntervalFormatterFullStyle , the duration has the following format: Thursday, March 5, 2015, 4:51:36 PM GMT-05:00 - Thursday, March 5, 2015, 4:52:01 PM GMT-05:00 . 如果我将格式化程序上的语言环境设置为nil并将日期和时间格式设置为NSDateIntervalFormatterFullStyle ,则duration具有以下格式: 2015年3月5日,星期四,格林尼治标准时间05:00-3月5日, ,2015,4:52:01 PM GMT-05:00

How can I format the duration between two dates with ISO8601 period format (or at least as "'P'yyyy'Y'M'M'd'DT'H'H'm'M's.S'S'" )? 如何使用ISO8601期间格式(或至少将其格式为“'P'yyyy'Y'M'M'd'DT'H'H'm'M's.S'S'” )格式化两个日期之间的持续时间?

The class you are using, NSDateIntervalFormatter , is designed to produce intervals of the form date1 - date2 , not format the time difference between two dates. 您使用的类NSDateIntervalFormatter旨在产生date1-date2格式的间隔 ,而不是格式化两个日期之间的时间差。

To get what you are after you need to use NSCalendar , NSDateComponents and stringWithFormat: (or similar). 要获得所需的信息,您需要使用NSCalendarNSDateComponentsstringWithFormat:或类似名称)。

  • First you need a calendar, [NSCalendar currentCalendar] is a good choice. 首先,您需要一个日历, [NSCalendar currentCalendar]是一个不错的选择。 (The difference between two absolute points in times can be a different when expressed in the units of a particular calendar - if that doesn't make sense don't worry, just use currentCalendar !) (当以特定日历的单位表示时,两个绝对时间点之间的差异可能会有所不同-如果没有意义,请放心,请使用currentCalendar !)

  • Next use the components:fromDate:toDate:options: method to obtain an instance of NSDateComponents which contains the number of years, days, hours etc. between your two dates. 接下来使用components:fromDate:toDate:options:方法来获取NSDateComponents实例,其中包含两个日期之间的年,日,小时等。

  • Finally use stringWithFormat: to format the components as you wish. 最后,使用stringWithFormat:格式化所需的组件。

HTH 高温超导

You have misunderstood what NSDateIntervalFormatter is for. 您误解了NSDateIntervalFormatter的用途。 It's for formatting an interval as a string of the form start - end . 它用于将间隔格式化为start-end形式的字符串。

It's not for formatting a duration. 它不是用于格式化持续时间。

You can use NSDateComponentsFormatter to format a duration. 您可以使用NSDateComponentsFormatter格式化持续时间。 However, I don't know if it supports the specific format you want. 但是,我不知道它是否支持您想要的特定格式。

It has the convenience methods -stringFromDate:toDate: and -stringFromTimeInterval: . 它具有便捷的方法-stringFromDate:toDate:-stringFromTimeInterval:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM