0

我有一个日期格式: Fri Jul 16 16:58:46 +0000 2010。要将其转换为2010 年 7 月 16 日星期五,我尝试了:

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)只写(null)。如何让它工作?

编辑无论出于何种原因,我通过更改使其工作

[df setDateFormat:@"eee MMM dd HH:mm:ss Z yyyy"];

[df setDateFormat:@"MMM dd HH:mm:ss Z yyyy"];

并从我的字符串中删除工作日。不过,谢谢大家。

4

3 回答 3

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];
于 2012-04-29T13:01:55.780 回答
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|Second, DATE_TYPE_MMDDYYYY将返回Month|Day|Year, DATE_TYPE_MMDDYYYYhhmmss将返回Month|Day|Year|Hour|Minute|Second, DATE_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
于 2012-04-29T18:55:02.880 回答
0

对周 EEE 使用 Dateformatter 而不是 eee

[df setDateFormat:@"EEE MMM dd HH:mm:ss Z yyyy"];

我希望这对你有帮助......

于 2012-04-30T06:16:54.680 回答