1

我正在尝试将日期字符串转换为另一种格式。我正在这样做。

NSLog(@"datestring is NOW %@",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-DD"];
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(@"date transformed %@",date);
[dateFormat setDateFormat:@"EEEE, dd/MM/YYYY"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(@"datestring is AFTER %@",dateStr);

但这是我从我的 NSLOG 中得到的

2013-04-18 08:43:36.181 mosaqua2[9629:907] datestring is NOW 2013-05-04
2013-04-18 08:43:36.184 mosaqua2[9629:907] date transformed 2013-01-03 23:00:00 +0000
2013-04-18 08:43:36.184 mosaqua2[9629:907] datestring is AFTER donderdag, 03/01/2013

我想要的是

Saterday, 04/05/2013

有谁能够帮我 ?

4

6 回答 6

2

使用我的以下自定义方法转换日期格式...

-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{


    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:dateFormat];

    NSDate *date = [dateFormatter dateFromString:stringDate];

    dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:getwithFormat];

    NSString *convertedString = [dateFormatter stringFromDate:date];
    NSLog(@"Converted String : %@",convertedString);
    return convertedString;
}

并像下面这样使用它......

NSString *strSDate = [self changeDateFormat:_dayObject.p_date dateFormat:@"yyyy-MM-dd" getwithFormat:@"EEEE, dd/MM/yyyy"];
NSLog(@"datestring is AFTER %@",strSDate);

请参阅我的博客与这篇文章...

于 2013-04-18T06:49:02.653 回答
1

检查这个......

NSLog(@"datestring is NOW ==>> %@", _dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

NSDate *date = [dateFormat dateFromString: _dayObject.p_date];
NSLog(@"date transformed ==>> %@",date);
[dateFormat setDateFormat:@"EEEE, dd/MM/yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(@"datestring is AFTER ==>> %@",dateStr);
  • 输出:~

    日期字符串现在 ==>> 2013-05-04

    转换日期 ==>> 2013-05-04 00:00:00 +0000

    日期字符串在 ==>> 星期六,2013 年 4 月 5 日之后

于 2013-04-18T06:57:27.897 回答
0

检查参数的字符大小写

NSLog(@"datestring is NOW %@",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; //MODIFIED
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(@"date transformed %@",date);
[dateFormat setDateFormat:@"EEEE, dd/MM/yyyy"]; //MODIFIED
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(@"datestring is AFTER %@",dateStr);
于 2013-04-18T06:49:11.833 回答
0

请尝试..这将有助于获得周名

  NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"eeee"];
 NSCalendar * cal = [NSCalendar currentCalendar];
NSUInteger numWeekdays = [cal maximumRangeOfUnit:NSWeekdayCalendarUnit].length;
 NSDateComponents * comp = [[NSDateComponents alloc] init];
for( NSUInteger day = 1; day <= numWeekdays; day++ ){
[comp setWeekday:day];
[comp setWeek:0];
NSDate * date = [cal dateFromComponents:comp];

NSString * dayName = [dateFormat stringFromDate:date];

NSLog(@"%@", dayName);
}
于 2013-04-18T06:53:28.110 回答
0

你做得很好,除了setTimeZone,评论你会得到你的输出。

NSLog(@"datestring is NOW %@",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-DD"];
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(@"date transformed %@",date);
[dateFormat setDateFormat:@"EEEE, dd/MM/YYYY"];
//[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; //NOT NEEDED
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(@"datestring is AFTER %@",dateStr);
于 2013-04-18T06:59:56.700 回答
0

改变这个:“YYYY”

致:“yyyy”

错误原因,

使用 YYYY 是一个常见的错误。yyyy 指定日历年,而 YYYY 指定年份(“一年中的一周”),用于 ISO 年-周日历。在大多数情况下,yyyy 和 YYYY 产生相同的数字,但它们可能不同。通常,您应该使用日历年。

于 2013-04-18T07:09:20.647 回答