如何重用NSString方法?

内普克

我正在写一个接受字符串并返回字符串的方法。我正在尝试以另一种方法使用它。

- (NSString*) formatDate:(NSString*) showStartDateTime
{
    NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"UTC"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:tz];
    [dateFormatter setDateFormat:@"EEE MM/dd/yyyy"];
    NSDate*dattt=[dateFormatter dateFromString:showStartDateTime ];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString*tempo2= [dateFormatter stringFromDate:dattt];
    return tempo2;

}

这就是我的使用方式。[self formatDate:@“ Fri 08/08/2014”]这是使用它的正确方法吗?

NSString *hellostring=[self formatDate:@"Fri 08/08/2014"];
NSLog(@"%@",hellostring);

我知道这行不通。任何建议表示赞赏。

北欧海盗第二

我写了一个命令行应用程序,但一直无法运行dateFromString:,直到结果为nil为止,直到我添加了dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

- (NSString*) formatDate:(NSString*) showStartDateTime
{
    NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"UTC"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:tz];

    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setDateFormat:@"EEE MM/dd/yyyy"];
    NSDate*dattt=[dateFormatter dateFromString:showStartDateTime ];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString*tempo2= [dateFormatter stringFromDate:dattt];
    return tempo2;

}

现在返回 2014-08-08

请参阅技术问答QA1480:NSDateFormatter和Internet日期

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章