开发者

NSString to NSDate to "15 min" or "1 hour" or "1day"

开发者 https://www.devze.com 2023-02-14 19:35 出处:网络
How would I go about doing this in obj c? NSString to NSDate to \"1 hour\" or \"1day\" or \"15 min\" Is there a library out that can do this?In Ruby its very easy... has to be some开发者_运维百科thi

How would I go about doing this in obj c?

NSString to NSDate to "1 hour" or "1day" or "15 min"

Is there a library out that can do this? In Ruby its very easy... has to be some开发者_运维百科thing in Obj c that can do the same thing?


Use NSDateFormatter to input/output your NSDate: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

Data Formatting Guide (chapter "Date Formatters"): 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
0

精彩评论

暂无评论...
验证码 换一张
取 消