【问题标题】:Date Converting [duplicate]日期转换[重复]
【发布时间】:2015-07-05 10:52:45
【问题描述】:

我有一个字符串30-07-2013,我想转换成30 June。 我正在使用

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"dd/MM/YYYY"];
 NSDate *date = [dateFormat dateFromString:@"30-07-1983"];

他们回复1984-12-24 18:06:32 +0000

【问题讨论】:

  • 这个问题在这里被问了上千次。你用谷歌搜索过吗?使用正确的日期格式。
  • 刚刚更新了Nandana的答案..
  • 关键是您使用的格式必须与您拥有的数据字符串的格式相匹配...

标签: ios objective-c iphone


【解决方案1】:

试试这个

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];
NSDate *date = [dateFormat dateFromString:@"30-07-2013"];

 [dateFormat setDateFormat:@"dd MMM"];
NSString *strDate=[dateFormat stringFromDate:date];
NSLog(@"strDate : %@",strDate);

【讨论】:

  • 您应该使用yyyy 而不是YYYY。两者都不一样
  • "dd MMM" 格式仅返回 3 个字符的月份名称。如果您想要月份的全名,请尝试以下代码。
  • @MalavSoni 要得到月份的全名,只要多加一个“M”,“MMMM”的日期格式就可以了
【解决方案2】:

请试试这个

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];
NSDate *date = [dateFormat dateFromString:@"30-07-1983"];

NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitDay | NSCalendarUnitMonth) fromDate:date];

NSLog(@"%d %@",(int)[components day],[[[NSDateFormatter new] monthSymbols] objectAtIndex:(components.month-1)]);

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 2011-05-23
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2018-04-12
    • 1970-01-01
    相关资源
    最近更新 更多