【问题标题】:Date formats in iOS?iOS中的日期格式?
【发布时间】:2014-12-12 15:47:16
【问题描述】:

在我的应用程序中,我使用'Thurs 10th April' 之类的日期格式。但我得到的日期格式类似于'Thurs 10 April'。我不知道如何更改此格式。

对于 1 月 1 日 - 它应该像 'Jan 1st'。 八月二日 - 它应该像'Aug 2nd'。 4 月 3 日 - 它应该像 'April 3rd'

我想要这种格式的日期。我是 Xcode 的新手。请任何人帮助解决这个问题。

【问题讨论】:

  • 请参考以下链接以获取您的解决方案。stackoverflow.com/questions/10103654/ios-date-formating
  • 你是 xcode 或 Objective-c 的新手??
  • 我是 Objective-c 的新手。
  • @iworld 你想要 4 月 10 日星期四 等格式的日期还是 4 月 10 日 等格式的日期?
  • 日期应该是 10 号。

标签: ios objective-c nsdate


【解决方案1】:

看看可爱但复杂的 NSDateFormatter 类。当你在那里时,认识她的朋友 NSLocale、NSTimeZone、NSCalendar、NSDate 和 NSDateComponents。还请务必阅读 Apple 提供的日期和时间编程指南(并再次阅读。)

【讨论】:

  • 它正在崩溃 [__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
  • StackExchange 上有大约一百个关于这个问题的问题。进行搜索。
【解决方案2】:

我假设您已准备好在可变字符串中使用您的日期。如果是这样,您可以使用以下内容来获得您想要的输出。

NSMutableString *mutableStrDate = // Your date in Mutable String Format.
int day = [[mutableStrDate substringFromIndex:[mutableStrDate length] - 2] intValue];

switch (day) 
{
    case 1:
    case 21:
    case 31:
        [mutableStrDate appendString:@"st"];
        break;
    case 2:
    case 22:
        [mutableStrDate appendString:@"nd"];
        break;
    case 3:
    case 23:
        [mutableStrDate appendString:@"rd"];
        break;
    default:
        [mutableStrDate appendString:@"th"];
        break;
}
NSLog(@"Formatted Date with %@",mutableStrDate);

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    你可以这样做:

    1. 将字符串分割成组件并存储在数组中。

    2. 从该数组中获取日期部分并对其进行一些检查。

    3. 在当天附加适当的日期后缀。

    4. 最后追加整个字符串。

    下面是代码:

    NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"EEE dd MMMM"];
    
        NSDate *now = [NSDate date];
    
        NSString *dateString = [format stringFromDate:now];
        NSArray*tempString1 = [dateString componentsSeparatedByString:@" "];
    
        NSMutableString *tempDate = [[NSMutableString alloc]initWithString:[tempString1 objectAtIndex:1]];
        int day = [[tempString1 objectAtIndex:1] intValue];
        NSLog(@"%d",day);
    
        switch (day) {
            case 1:
                case 21:
            case 31:
                [tempDate appendString:@"st"];
                break;
            case 2:
                case 22:
                [tempDate appendString:@"nd"];
                break;
            case 3:
                case 23:
                [tempDate appendString:@"rd"];
                break;
            default:
                [tempDate appendString:@"th"];
                break;
        }
        NSLog(@"%@",tempDate);
    
        NSString *str1 = [[tempString1 objectAtIndex:0] stringByAppendingString:@" "];
    
        NSString *str2 = [str1 stringByAppendingString:tempDate];
        NSString *str3 = [str2 stringByAppendingString:@" "];
    
        NSString *finalDate = [str3 stringByAppendingString:[tempString1 objectAtIndex:2]];
        NSLog(@"%@",finalDate);
    

    【讨论】:

    • @iworld 很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多