【问题标题】:Set DateFormatter [duplicate]设置 DateFormatter [重复]
【发布时间】:2013-03-25 09:43:59
【问题描述】:

我有一些这样的代码:

NSString *dateStr = @"23 03 2013";
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:@"d MM yyyy"];
NSDate *date1 = [df1 dateFromString:dateStr];
NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
[df2 setDateFormat:@"MMMM d YYYY"];
NSString *s = [df2 stringFromDate:date1];
NSLog(@"my new Date is %@",s);

//Output : March 23 2013

但我想要这样:2013 年 3 月 23 日,如何解决?

【问题讨论】:

  • 如果可能,不要像这样格式化日期。首先,选择非英语语言环境时会破坏。其次,英语没有明确地将序数写到日期。在大多数情况下,使用setDateStyle: 设置格式比使用setDateFormat: 更适合和可移植。 setDateFormat: 最好与解析一起使用,而不是在向用户显示日期时使用。

标签: ios nsdateformatter


【解决方案1】:

用途:

[df2 setDateFormat:@"MMMM d'rd' YYYY"];

*注意,对于 21st、22nd、rd、th 等,你需要检查使用条件,然后更改格式化程序

NSString *dateStr = @"23 03 2013";
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:@"d MM yyyy"];
NSDate *date1 = [df1 dateFromString:dateStr];
NSDateFormatter *df2 = [[NSDateFormatter alloc] init];

NSDateFormatter *dfDate=[NSDateFormatter new];
[dfDate setDateFormat:@"dd"];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date1];


NSInteger dayValue=[components day];
/*
switch (dayValue) {
    case 1:
    case 21:
    case 31:
        [df2 setDateFormat:@"MMMM d'st' YYYY"];
        break;
    case 2:
    case 22:
        [df2 setDateFormat:@"MMMM d'nd' YYYY"];
        break;
    case 3:
    case 23:
        [df2 setDateFormat:@"MMMM d'rd' YYYY"];
        break;
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
    case 24:
    case 25:
    case 26:
    case 27:
    case 28:
    case 29:
    case 30:
        [df2 setDateFormat:@"MMMM d'th' YYYY"];
        break;

    default:
        break;
}
*/

NSArray *suffix=@[@"st",@"nd",@"rd",
              @"th",@"th",@"th",@"th",@"th",@"th",@"th",
              @"th",@"th",@"th",@"th",@"th",@"th",@"th",@"th",@"th",
              @"th",
              @"st",@"nd",@"rd",@"th",@"th",@"th",@"th",@"th",@"th",@"th",@"st"];
NSLog(@"%d",[suffix count]);

[df2 setDateFormat:[NSString stringWithFormat:@"MMMM d'%@' YYYY",suffix[dayValue - 1]]];

NSString *s = [df2 stringFromDate:date1];
NSLog(@"my new Date is %@",s);

注意:我使用了 switch,你可以使用数组来重构它。

现在可以

【讨论】:

【解决方案2】:

直接在IOS中不可用,但我使用以下代码实现了它

NSString *dateStr = @"23 03 2013";
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:@"d MM yyyy"];
NSDate *date1 = [df1 dateFromString:dateStr];
NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
[df2 setDateFormat:@"MMMM d YYYY"];
NSString *s = [df2 stringFromDate:date1];

NSDateFormatter *monthDayFormatter = [[NSDateFormatter alloc] init];
[monthDayFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[monthDayFormatter setDateFormat:@"d"];
int date_day = [[monthDayFormatter stringFromDate:date1] intValue];
NSString *suffix_string = @"|st|nd|rd|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|st|nd|rd|th|th|th|th|th|th|th|st";
NSArray *suffixes = [suffix_string componentsSeparatedByString: @"|"];
NSString *suffix = [suffixes objectAtIndex:date_day];

NSLog(@"Suffix %@",suffix);

【讨论】:

  • @DipenPanchasara 没有人投反对票,尽管抄袭肯定是投反对票的一个好理由。如果您的答案基于其他人的内容,您必须给予他们信任。
  • 对不起,安德鲁。我会的。
  • @DipenPanchasara 如果你的答案来自某人的工作,你不认为你应该给予他们信任吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多