2012-04-29 4 views
0

日付は、 金7月16日16:58:46 +0000 2010です。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)のみを書き込む(ヌル): は、私が試した金2010年7月16日に変換します。それを働かせるには?私はそれが

を変更することにより、作業しまっ何らかの理由[:@ ":MM:EEE MMMはHH ddはSSのZのYYYYを" DFのsetDateFormat]の場合

EDIT 。

[DFのsetDateFormat: ":MM:MMMはHH ddは、SSのZのYYYYを" @]。

そして、私の文字列から平日を削除します。しかし、ありがとうございました。

+1

これはおそらく、文字列を変数 'date'に解析できませんでした。 'NSLog(@"%@ "、日付)'を実行して、それがnilかどうかを確認してください。 – joerick

+0

@joerick:それはnil(null)です。 –

+0

@sch: "**日付は0日です。Apr 29 11:11:22 +0000 2012 **"などのように –

答えて

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; 
} 

から日付を作成します。 通常は読みにくいですNSDateオブジェクトを返すので、あなたが列を取得するために[self getStringFromDate:date dateType:DATE_TYPE_MMDDYYYY]を使用することができます[self getDateWithMonth:12 day:24 year:1994]方法日

-(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); 

から文字列を取得します。 NSDateオブジェクトです。

定義(マクロ)を使用して、文字列に入れたい日付の形式を指定します。例えば

DATE_TYPE_hhmmssMonth|Day|YearDATE_TYPE_MMDDYYYYhhmmssが戻ってくるHour|Minute|SecondDATE_TYPE_MMDDYYYYが戻ってくる戻ってくるMonth|Day|Year|Hour|Minute|SecondDATE_TYPE_MMDDYYYYWWhhmmssMonth|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素晴らしいですが、私はすでにそれが働いている。 –

0

日付を解析している元の文字列が実際には "20081122"の形式である場合、指定された形式が正しくないため、両方のスニペットで "setDateFormat"の最初の呼び出しが正しくありません。

[celldata objectForKey:@ "created_at"]が2番目のコードスニペットで "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

申し訳ありませんが、編集部分に間違いがありました。それを無視してください。 –

0

使用したDateFormatter週EEEの代わりにEEEため

[setDateFormat DF: ":MM:EEE MMMはHH ddをSSのZのYYYYを" @]。

これはあなたに役立つことを願っています...

関連する問題