【问题标题】:Date Format Change From 2013-12-30T14:59:00.+0000日期格式更改从 2013-12-30T14:59:00.+0000
【发布时间】:2014-02-03 18:14:41
【问题描述】:

我有一个来自服务器的日期格式为 2013-12-30T14:59:00.+0000 我想将其转换为格式 MMM dd,yyyy h:mm a

我的做法如下

NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
[dateFormatter setTimeZone:timeZone];
NSDate *dateFromString = [dateFormatter dateFromString:selectedDate];
if (dateFromString != nil) 
{
       [dateFormatter setDateFormat:@"MMM dd,yyyy h:mm a"];
       stringDateFromDate = [dateFormatter stringFromDate:dateFromString];
}

但“dateFromString”为 nil。

谁能帮帮忙

【问题讨论】:

    标签: nsdateformatter


    【解决方案1】:

    在我看来,现在提出建议为时已晚。但我决定写。

    为您查看此示例 selectedDate 字符串并执行以下操作:

    NSString *selectedDate = @"2013-12-30T14:59:00.+0000";
    
    // I donno what "T" means, that's just replace it to an empty string
    selectedDate = [selectedDate stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    
    // get string without .+0000
    NSRange range = [selectedDate rangeOfString:@"."];
    selectedDate = [selectedDate substringToIndex:range.location];
    
    NSString *stringDateFromDate;
    
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    
    // necessarily set this date format!
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateFromString = [dateFormatter dateFromString:selectedDate];
    
    if (dateFromString != nil)
    {
    [dateFormatter setDateFormat:@"MMM dd,yyyy h:mm a"];
    stringDateFromDate = [dateFormatter stringFromDate:dateFromString];
    }
    
    // That's all. Output your string.
    NSLog(@"%@", stringDateFromDate);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多