从字符串解析NSDate

时间:2013-08-13 13:58:45

标签: string nsdate

方法dateFromString始终返回大于一小时的时间。例子:

String“2013-08-31 00 :00:00” - > NSDate“2013-08-31 01 :00:00 MSK”

String“2013-08-31 11 :59:59” - > NSDate“2013-08-31 12 :59:59 MSK”

我试试这个:

[dateFormat setTimeZone:[NSTimeZone localTimeZone]]; and 
[dateFormat setTimeZone:[NSTimeZone defaultTimeZone]];

结果是一样的......

而且:

[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

String“2013-08-31 00 :00:00” - > NSDate“2013-08-31 05 :00:00 MSK”(+4小时)

我需要得到字符串中的相同时间:

String“2013-08-31 00:00:00” - > NSDate“2013-08-31 00:00:00 MSK”

我该怎么做?

1 个答案:

答案 0 :(得分:0)

使用跟随它对我来说工作正常:

+(NSString *)getDateInStringFormat:(NSDate *)creationDate {

    NSDateFormatter *createDateFormatter = [[NSDateFormatter alloc]init];
    [createDateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    [createDateFormatter setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss"];
    NSString *currentDateString = [createDateFormatter stringFromDate:creationDate];
    //NSLog(@"Prfile creation date is = %@",ProfileCreationDate);
    return currentDateString;
}