NSString到NSDate到“15分钟”或“1小时”或“1天”

时间:2011-03-03 14:16:41

标签: objective-c nsstring nsdate

我如何在对象中执行此操作?

NSString到NSDate到“1小时”或“1天”或“15分钟”

是否有可以执行此操作的库?在Ruby中,它很容易......在Obj c中可以做同样的事情吗?

1 个答案:

答案 0 :(得分:1)

使用NSDateFormatter输入/输出您的NSDate: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

数据格式指南(“日期格式化程序”一章): http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1

NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];

[inputFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];



NSDate *formatterDate = [inputFormatter dateFromString:@"1999-07-11 at 10:30"];



NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];

[outputFormatter setDateFormat:@"HH:mm 'on' EEEE MMMM d"];



NSString *newDateString = [outputFormatter stringFromDate:formatterDate];



NSLog(@"newDateString %@", newDateString);

// For US English, the output is:

// newDateString 10:30 on Sunday July 11