2012-04-29 81 views
0

我有一個格式的日期: Fri Jul 16 16:58:46 +0000 2010。 要將其轉換爲週五2010年7月16日我想:iPhone上的格式日期返回(null)

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 

for(int i=0; i<[self.data count]; i++) { 
    id celldata = [self.data objectAtIndex:i]; 
    NSString *str = [NSString stringWithFormat:@"%@", [celldata objectForKey:@"created_at"]]; 
    NSLog(@"date for %u is %@",i, str); //this works and writes the date 
    [df setDateFormat:@"eee MMM dd HH:mm:ss Z yyyy"]; 
    NSDate *date = [df dateFromString:str]; 
    [df setDateFormat:@"eee MMM dd yyyy"]; 
    NSString *dateStr = [df stringFromDate:date]; 
    NSLog(@"%@",dateStr); 
} 

的NSLog(@ 「%@」,dateStr)只寫(空)。如何讓它工作?

EDIT 無論出於何種原因我知道了通過改變

工作[DF setDateFormat:@ 「EEE MMM DD HH:MM:SSžYYYY」];

[DF setDateFormat:@ 「MMM DD HH:MM:SSžYYYY」];

並從我的字符串中刪除星期幾。不過,謝謝大家。

+1

它可能無法將字符串解析到變量'date'中。嘗試做'NSLog(@「%@」,日期)'看看是否是零。 – joerick 2012-04-29 10:53:12

+0

@joerick:它是零(null)。 – 2012-04-29 11:13:05

+0

@sch:「**日期爲0是Sun年04月29 11點11分22秒+0000 2012 **」等 – 2012-04-29 11:14:02

回答

0

這可能會幫助你有點。我有一個類似的問題,所以我創建了以下方法。


定義

#define DATE_TYPE_hhmmss [NSArray arrayWithObjects:@"h", @"m", @"s", nil] 
#define DATE_TYPE_MMDDYYYY [NSArray arrayWithObjects:@"M", @"D", @"Y", nil] 
#define DATE_TYPE_MMDDYYYYhhmmss [NSArray arrayWithObjects:@"M", @"D", @"Y", @"h", @"m", @"s", nil] 
#define DATE_TYPE_MMDDYYYYWWhhmmss [NSArray arrayWithObjects:@"M", @"D", @"Y", @"W", @"h", @"m", @"s", nil] 
#define DATE_TYPE_MMDDYYYYhhmmssWW [NSArray arrayWithObjects:@"M", @"D", @"Y", @"h", @"m", @"s", @"W", nil] 
#define DATE_TYPE_YYYYMMDD [NSArray arrayWithObjects:@"Y", @"M", @"D", nil] 
#define DATE_TYPE_YYYYMMDDhhmmss [NSArray arrayWithObjects:@"Y", @"M", @"D", @"h", @"m", @"s", nil] 
#define DATE_TYPE_YYYYMMDDWWhhmmss [NSArray arrayWithObjects:@"Y", @"M", @"D", @"W", @"h", @"m", @"s", nil] 
#define DATE_TYPE_YYYYMMDDhhmmssWW [NSArray arrayWithObjects:@"Y", @"M", @"D", @"h", @"m", @"s", @"W", nil] 
#define DATE_TYPE_FRIENDLY [NSArray arrayWithObjects:@"xx", nil] 

日期方法

創建日期從價值觀

-(NSDate *) getDateWithMonth:(int)month day:(int)day year:(int)year { 
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; 
    NSDateComponents * dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]]; 

    [dateFormatter setDateFormat:@"MM"]; 
    [dateComponents setMonth:month]; 
    [dateFormatter setDateFormat:@"DD"]; 
    [dateComponents setDay:day]; 
    [dateFormatter setDateFormat:@"YYYY"]; 
    [dateComponents setYear:year]; 

    NSDate * result = [calendar dateFromComponents:dateComponents]; 

    return result; 
} 

-(NSDate *) getDateWithMonth:(int)month day:(int)day year:(int)year hour:(int)hour minute:(int)minute { 
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; 
    NSDateComponents * dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; 

    [dateFormatter setDateFormat:@"MM"]; 
    [dateComponents setMonth:month]; 
    [dateFormatter setDateFormat:@"DD"]; 
    [dateComponents setDay:day]; 
    [dateFormatter setDateFormat:@"YYYY"]; 
    [dateComponents setYear:year]; 
    [dateFormatter setDateFormat:@"HH"]; 
    [dateComponents setHour:hour]; 
    [dateFormatter setDateFormat:@"MM"]; 
    [dateComponents setMinute:minute]; 

    NSDate * result = [calendar dateFromComponents:dateComponents]; 

    return result; 
} 

-(NSDate *) getDateWithMonth:(int)month day:(int)day year:(int)year hour:(int)hour minute:(int)minute second:(int)second { 
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; 
    NSDateComponents * dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]]; 

    [dateFormatter setDateFormat:@"MM"]; 
    [dateComponents setMonth:month]; 
    [dateFormatter setDateFormat:@"DD"]; 
    [dateComponents setDay:day]; 
    [dateFormatter setDateFormat:@"YYYY"]; 
    [dateComponents setYear:year]; 
    [dateFormatter setDateFormat:@"HH"]; 
    [dateComponents setHour:hour]; 
    [dateFormatter setDateFormat:@"MM"]; 
    [dateComponents setMinute:minute]; 
    [dateFormatter setDateFormat:@"SS"]; 
    [dateComponents setSecond:second]; 

    NSDate * result = [calendar dateFromComponents:dateComponents]; 

    return result; 
} 

獲取字符串從日期

-(NSString *) getStringFromDate:(NSDate *)date dateType:(NSArray *)dateType { 
    NSString * result = @""; 

    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSString * format = @""; 

    NSDateComponents * dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:date]; 

    NSInteger year = [dateComponents year]; 
    NSInteger month = [dateComponents month]; 
    NSInteger day = [dateComponents day]; 
    NSInteger weekday = [dateComponents weekday]; 
    NSInteger hour = [dateComponents hour]; 
    NSInteger minute = [dateComponents minute]; 
    NSInteger second = [dateComponents second]; 

    if (dateType != nil) { 
     for (int x = 0; x < [dateType count]; x++) { 
      if (x == ([dateType count]-1)) { 
       if ([[dateType objectAtIndex:x] isEqualToString:@"Y"]) { 
        format = [format stringByAppendingFormat:@"%d", year]; 
       } else if ([[dateType objectAtIndex:x] isEqualToString:@"M"]) { 
        format = [format stringByAppendingFormat:@"%d", month]; 
       } else if ([[dateType objectAtIndex:x] isEqualToString:@"D"]) { 
        format = [format stringByAppendingFormat:@"%d", day]; 
       } else if ([[dateType objectAtIndex:x] isEqualToString:@"W"]) { 
        format = [format stringByAppendingFormat:@"%d", weekday]; 
       } else if ([[dateType objectAtIndex:x] isEqualToString:@"h"]) { 
        format = [format stringByAppendingFormat:@"%d", hour]; 
       } else if ([[dateType objectAtIndex:x] isEqualToString:@"m"]) { 
        format = [format stringByAppendingFormat:@"%d", minute]; 
       } else if ([[dateType objectAtIndex:x] isEqualToString:@"s"]) { 
        format = [format stringByAppendingFormat:@"%d", second]; 
       } 
      } else { 
       if ([[dateType objectAtIndex:x] isEqualToString:@"Y"]) { 
        format = [format stringByAppendingFormat:@"%d|", year]; 
       } else if ([[dateType objectAtIndex:x] isEqualToString:@"M"]) { 
        format = [format stringByAppendingFormat:@"%d|", month]; 
       } else if ([[dateType objectAtIndex:x] isEqualToString:@"D"]) { 
        format = [format stringByAppendingFormat:@"%d|", day]; 
       } else if ([[dateType objectAtIndex:x] isEqualToString:@"W"]) { 
        format = [format stringByAppendingFormat:@"%d|", weekday]; 
       } else if ([[dateType objectAtIndex:x] isEqualToString:@"h"]) { 
        format = [format stringByAppendingFormat:@"%d|", hour]; 
       } else if ([[dateType objectAtIndex:x] isEqualToString:@"m"]) { 
        format = [format stringByAppendingFormat:@"%d|", minute]; 
       } else if ([[dateType objectAtIndex:x] isEqualToString:@"s"]) { 
        format = [format stringByAppendingFormat:@"%d|", second]; 
       } 
      } 

      if ([[dateType objectAtIndex:x] isEqualToString:@"xx"]) { 
       format = [NSString stringWithFormat:@"Year: %d, Month: %d, Day: %d, Weekday: %d, Hour: %d, Minute: %d, Second: %d", year, month, day, weekday, hour, minute, second]; 
      } 
     } 
    } else { 
     format = [format stringByAppendingFormat:@"%d|", year]; 
     format = [format stringByAppendingFormat:@"%d|", month]; 
     format = [format stringByAppendingFormat:@"%d|", day]; 
     format = [format stringByAppendingFormat:@"%d|", weekday]; 
     format = [format stringByAppendingFormat:@"%d|", hour]; 
     format = [format stringByAppendingFormat:@"%d|", minute]; 
     format = [format stringByAppendingFormat:@"%d|", second]; 

     format = [NSString stringWithFormat:@"%d|%d|%d|%d|%d|%d|%d", year, month, day, weekday, hour, minute, second]; 
    } 

    result = format; 

    return result; 
} 

NSDate * date = [self getDateWithMonth:12 day:24 year:1994]; 
NSString * dateInString = [self getStringFromDate:date dateType:DATE_TYPE_MMDDYYYY]; 

int month = [[[dateInString componentsSeparatedByString:@"|"] objectAtIndex:0] intValue]; 
int day = [[[dateInString componentsSeparatedByString:@"|"] objectAtIndex:1] intValue]; 
int year = [[[dateInString componentsSeparatedByString:@"|"] objectAtIndex:2] intValue]; 

NSLog(@"String of Date: \"%@\"", dateInString); 
NSLog(@"Month: %d", month); 
NSLog(@"Day: %d", day); 
NSLog(@"Year: %d", year); 

[self getDateWithMonth:12 day:24 year:1994]返回一個NSDate對象通常是難以閱讀,因此,您可以使用[self getStringFromDate:date dateType:DATE_TYPE_MMDDYYYY]得到的字符串的方法一個NSDate對象。

使用的定義(宏)來指定日期的格式要得到的字符串中。

例如: DATE_TYPE_hhmmss將返回Hour|Minute|SecondDATE_TYPE_MMDDYYYY將返回Month|Day|YearDATE_TYPE_MMDDYYYYhhmmss將返回Month|Day|Year|Hour|Minute|SecondDATE_TYPE_MMDDYYYYWWhhmmss將返回Month|Day|Year|Weekday (#)|Hour|Minute|Second

等等...

控制檯日誌

2012-04-29 13:42:15.791 Atomic Class[1373:f803] String of Date: "12|24|1994" 
2012-04-29 13:42:15.793 Atomic Class[1373:f803] Month: 12 
2012-04-29 13:42:15.794 Atomic Class[1373:f803] Day: 24 
2012-04-29 13:42:15.794 Atomic Class[1373:f803] Year: 1994 
+0

感謝thats真棒,但我已經得到它的工作。 – 2012-04-29 20:45:25

0

如果您解析到日期的原始字符串確實格式爲「20081122」,則第一次對「setDateFormat」的調用在兩個片段中都不正確,因爲指定的格式不正確。

假設[celldata objectForKey:@「created_at」]是按您的第二代碼片段返回日期中的「20081122」的格式,您需要更改到setDateFormat第一次調用使用正確的格式字符串「 yyyyMMdd「當您調用方法」dateFromString「時,這將驅動正確的轉換然後,一旦您有NSDate *對象表示形式,您可以在通過stringFromDate將其轉換回字符串時使用任何需要的格式。

NSString *dateStr = @"20081122"; 
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
//This is the important change here, this format MUST match the format of the string. 
[dateFormat setDateFormat:@"yyyyMMdd"]; 
NSDate *date = [dateFormat dateFromString:dateStr]; 
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"]; 
dateStr = [dateFormat stringFromDate:date]; 
[dateFormat release]; 
+0

抱歉編輯部分有錯誤。請忽略它 – 2012-04-29 18:35:06

0

使用Dateformatter一週EEE代替EEE

[DF setDateFormat:@ 「EEE MMM DD HH:MM:SSžYYYY」];

我希望這會對你有所幫助...