4

下面是我的代码

 NSString *dateandtime =[NSString stringWithFormat:@"%@",self.eventtime];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:dateandtime];

如果我打印我的 self.event 名称,我将得到输出,Mon, 7 Jan 2013 15:30:00 CST但是当我传入这个字符串时,NSDateFormatter我得到了null价值,我不知道为什么我尝试过many combinationsdate formats但我无法解决它,任何人都可以帮助我

4

4 回答 4

1

我按照 Prateek 的评论解决了我的问题,现在它为我工作了……谢谢 Prateek。

in your self.event, you are getting CST, it should be time zone for example +0530
于 2013-01-08T13:19:41.407 回答
0

采用 :

[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
于 2013-01-08T08:31:07.313 回答
0

使用以下方法获取NSDate起始NSString日期

NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [self convertStringToDate:dateandtime];

将以下方法粘贴到您的.m文件中...

-(NSDate *)convertStringToDate:(NSString *) date {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
        NSDate *nowDate = [[[NSDate alloc] init] autorelease];
        [formatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss 'CST'"];// set format here  format in which the date should be

        date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
        nowDate = [formatter dateFromString:date];
        // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
        return nowDate;
}

有关NSDate组件的更多信息,请参阅我的博客NSDate-formate-info-for-iphone-sdk 的这篇文章

于 2013-01-08T09:11:00.290 回答
0

使用此代码...

- (NSString *)formattedDate:(NSString*)dateSte {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [NSTimeZone resetSystemTimeZone];
    NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
    [dateFormatter setTimeZone:gmtZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    NSDate *date = [dateFormatter dateFromString:dateSte];

    [dateFormatter setDateFormat:@"EEE,MMM d,''yyyy"];
    NSString *strDate = [dateFormatter stringFromDate:date];

    return strDate;
}
于 2013-01-08T10:31:50.080 回答